class Chewy::Search::Response

This class is a ES response hash wrapper.

@see www.elastic.co/guide/en/elasticsearch/reference/current/_the_search_api.html

Public Class Methods

new(body, loader, paginator = nil) click to toggle source

@param body [Hash] response body hash @param loader [Chewy::Search::Loader] loader instance

# File lib/chewy/search/response.rb, line 9
def initialize(body, loader, paginator = nil)
  @body = body
  @loader = loader
  @paginator = paginator
end

Public Instance Methods

aggregations()
Alias for: aggs
aggs() click to toggle source

The ‘aggregations` response part. Returns empty hash if aggregations were not requested.

@return [Hash]

# File lib/chewy/search/response.rb, line 62
def aggs
  @aggs ||= @body['aggregations'] || {}
end
Also aliased as: aggregations
document_hash()
Alias for: object_hash
documents()
Alias for: objects
hits() click to toggle source

Raw response ‘hits` collection. Returns empty array is something went wrong.

@return [Array<Hash>]

# File lib/chewy/search/response.rb, line 18
def hits
  @hits ||= hits_root['hits'] || []
end
max_score() click to toggle source

Response ‘max_score` field.

@return [Float]

# File lib/chewy/search/response.rb, line 32
def max_score
  @max_score ||= hits_root['max_score']
end
object_hash() click to toggle source

This method is used in cases when you need to iterate through both of the collections simultaneously.

@example

scope.each do |wrapper|
  scope.object_hash[wrapper]
end

@see wrappers @see objects @return [{Chewy::Index => Object}] a hash with wrappers as keys and ORM/ODM objects as values

# File lib/chewy/search/response.rb, line 106
def object_hash
  @object_hash ||= wrappers.zip(objects).to_h
end
Also aliased as: record_hash, document_hash
objects() click to toggle source

ORM/ODM objects that had been a source for Chewy import and now loaded from the DB using hits ids. Uses {Chewy::Search::Request#load} passed options for loading.

@see Chewy::Search::Request#load @see Chewy::Search::Loader @return [Array<Object>]

# File lib/chewy/search/response.rb, line 83
def objects
  @objects ||= begin
    objects = @loader.load(hits)
    if @paginator
      @paginator.call(objects)
    else
      objects
    end
  end
end
Also aliased as: records, documents
record_hash()
Alias for: object_hash
records()
Alias for: objects
suggest() click to toggle source

The ‘suggest` response part. Returns empty hash if suggests were not requested.

@return [Hash]

# File lib/chewy/search/response.rb, line 54
def suggest
  @suggest ||= @body['suggest'] || {}
end
timed_out?() click to toggle source

Has the request been timed out?

@return [true, false]

# File lib/chewy/search/response.rb, line 46
def timed_out?
  @timed_out ||= @body['timed_out']
end
took() click to toggle source

Duration of the request handling in ms according to ES.

@return [Integer]

# File lib/chewy/search/response.rb, line 39
def took
  @took ||= @body['took']
end
total() click to toggle source

Response ‘total` field. Returns `0` if something went wrong.

@return [Integer]

# File lib/chewy/search/response.rb, line 25
def total
  @total ||= hits_root.fetch('total', {}).fetch('value', 0)
end
wrappers() click to toggle source

{Chewy::Index} wrappers collection instantiated on top of hits.

@return [Array<Chewy::Index>]

# File lib/chewy/search/response.rb, line 70
def wrappers
  @wrappers ||= hits.map do |hit|
    @loader.derive_index(hit['_index']).build(hit)
  end
end

Private Instance Methods

hits_root() click to toggle source
# File lib/chewy/search/response.rb, line 114
def hits_root
  @body.fetch('hits', {})
end