debian-mirror-gitlab/elasticsearch-model/lib/elasticsearch/model/response.rb

85 lines
2 KiB
Ruby
Raw Normal View History

2019-12-22 22:52:31 +05:30
module Elasticsearch
module Model
# Contains modules and classes for wrapping the response from Elasticsearch
#
module Response
# Encapsulate the response returned from the Elasticsearch client
#
# Implements Enumerable and forwards its methods to the {#results} object.
#
class Response
2020-03-13 15:44:24 +05:30
attr_reader :klass, :search
2019-12-22 22:52:31 +05:30
include Enumerable
delegate :each, :empty?, :size, :slice, :[], :to_ary, to: :results
def initialize(klass, search, options={})
@klass = klass
@search = search
end
# Returns the Elasticsearch response
#
# @return [Hash]
#
def response
2020-03-13 15:44:24 +05:30
@response ||= HashWrapper.new(search.execute!)
2019-12-22 22:52:31 +05:30
end
# Returns the collection of "hits" from Elasticsearch
#
# @return [Results]
#
def results
@results ||= Results.new(klass, self)
end
# Returns the collection of records from the database
#
# @return [Records]
#
def records(options = {})
@records ||= Records.new(klass, self, options)
end
# Returns the "took" time
#
def took
2020-03-13 15:44:24 +05:30
raw_response['took']
2019-12-22 22:52:31 +05:30
end
# Returns whether the response timed out
#
def timed_out
2020-03-13 15:44:24 +05:30
raw_response['timed_out']
2019-12-22 22:52:31 +05:30
end
# Returns the statistics on shards
#
def shards
2020-03-13 15:44:24 +05:30
@shards ||= response['_shards']
2019-12-22 22:52:31 +05:30
end
# Returns a Hashie::Mash of the aggregations
#
def aggregations
2020-03-13 15:44:24 +05:30
@aggregations ||= Aggregations.new(raw_response['aggregations'])
2019-12-22 22:52:31 +05:30
end
# Returns a Hashie::Mash of the suggestions
#
def suggestions
2020-03-13 15:44:24 +05:30
@suggestions ||= Suggestions.new(raw_response['suggest'])
end
def raw_response
@raw_response ||= @response ? @response.to_hash : search.execute!
2019-12-22 22:52:31 +05:30
end
end
end
end
end