Sha256: 52627f151330dc144f4817d9e45b2a69a00a7de3a9eb495281692d0cb7133e80

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

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
        attr_reader :klass, :search, :response,
                    :took, :timed_out, :shards

        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
          @response ||= begin
            HashWrapper.new(search.execute!)
          end
        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
          response['took']
        end

        # Returns whether the response timed out
        #
        def timed_out
          response['timed_out']
        end

        # Returns the statistics on shards
        #
        def shards
          HashWrapper.new(response['_shards'])
        end

        # Returns a Hashie::Mash of the aggregations
        #
        def aggregations
          Aggregations.new(response['aggregations'])
        end

        # Returns a Hashie::Mash of the suggestions
        #
        def suggestions
          Suggestions.new(response['suggest'])
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
elasticsearch-model-6.0.0.pre lib/elasticsearch/model/response.rb
elasticsearch-model-5.1.0 lib/elasticsearch/model/response.rb
elasticsearch-model-5.0.2 lib/elasticsearch/model/response.rb
elasticsearch-model-5.0.1 lib/elasticsearch/model/response.rb
elasticsearch-model-5.0.0 lib/elasticsearch/model/response.rb