Class: Chewy::Search::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/search/response.rb

Overview

This class is a ES response hash wrapper.

Instance Method Summary collapse

Constructor Details

#initialize(body, loader, paginator = nil) ⇒ Response

Returns a new instance of Response.

Parameters:



9
10
11
12
13
# File 'lib/chewy/search/response.rb', line 9

def initialize(body, loader, paginator = nil)
  @body = body
  @loader = loader
  @paginator = paginator
end

Instance Method Details

#aggsHash Also known as: aggregations

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

Returns:

  • (Hash)


62
63
64
# File 'lib/chewy/search/response.rb', line 62

def aggs
  @aggs ||= @body['aggregations'] || {}
end

#hitsArray<Hash>

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

Returns:

  • (Array<Hash>)


18
19
20
# File 'lib/chewy/search/response.rb', line 18

def hits
  @hits ||= hits_root['hits'] || []
end

#max_scoreFloat

Response max_score field.

Returns:

  • (Float)


32
33
34
# File 'lib/chewy/search/response.rb', line 32

def max_score
  @max_score ||= hits_root['max_score']
end

#object_hash{Chewy::Index => Object} Also known as: record_hash, document_hash

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

Examples:

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

Returns:

  • ({Chewy::Index => Object})

    a hash with wrappers as keys and ORM/ODM objects as values

See Also:



106
107
108
# File 'lib/chewy/search/response.rb', line 106

def object_hash
  @object_hash ||= wrappers.zip(objects).to_h
end

#objectsArray<Object> Also known as: records, documents

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.

Returns:

  • (Array<Object>)

See Also:



83
84
85
86
87
88
89
90
91
92
# 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

#suggestHash

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

Returns:

  • (Hash)


54
55
56
# File 'lib/chewy/search/response.rb', line 54

def suggest
  @suggest ||= @body['suggest'] || {}
end

#timed_out?true, false

Has the request been timed out?

Returns:

  • (true, false)


46
47
48
# File 'lib/chewy/search/response.rb', line 46

def timed_out?
  @timed_out ||= @body['timed_out']
end

#tookInteger

Duration of the request handling in ms according to ES.

Returns:

  • (Integer)


39
40
41
# File 'lib/chewy/search/response.rb', line 39

def took
  @took ||= @body['took']
end

#totalInteger

Response total field. Returns 0 if something went wrong.

Returns:

  • (Integer)


25
26
27
# File 'lib/chewy/search/response.rb', line 25

def total
  @total ||= hits_root.fetch('total', {}).fetch('value', 0)
end

#wrappersArray<Chewy::Index>

Index wrappers collection instantiated on top of hits.

Returns:



70
71
72
73
74
# File 'lib/chewy/search/response.rb', line 70

def wrappers
  @wrappers ||= hits.map do |hit|
    @loader.derive_index(hit['_index']).build(hit)
  end
end