Sha256: 444a155b8b23dd4d18b3c61fae71dc6ff19e252c23b28a7315cd32ab65d31d6d

Contents?: true

Size: 1.05 KB

Versions: 15

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Esse
  module Search
    class Response
      include Enumerable
      extend Forwardable

      def_delegators :hits, :each, :size, :empty?
      attr_reader :query, :raw_response, :options

      # @param [Esse::Search::Query] query The search query
      # @param [Hash] raw_response The raw response from Elasticsearch
      # @param [Hash] options The options passed to the search
      def initialize(query, raw_response, **options)
        @query = query
        @raw_response = raw_response
        @options = options
      end

      def shards
        raw_response['_shards']
      end

      def aggregations
        raw_response['aggregations']
      end

      def suggestions
        raw_response['suggest']
      end

      def hits
        raw_response.dig('hits', 'hits') || []
      end

      def total
        if raw_response.dig('hits', 'total').respond_to?(:keys)
          raw_response.dig('hits', 'total', 'value')
        else
          raw_response.dig('hits', 'total')
        end.to_i
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
esse-0.4.0.rc4 lib/esse/search/response.rb
esse-0.4.0.rc3 lib/esse/search/response.rb
esse-0.4.0.rc2 lib/esse/search/response.rb
esse-0.4.0.rc1 lib/esse/search/response.rb
esse-0.3.5 lib/esse/search/response.rb
esse-0.3.4 lib/esse/search/response.rb
esse-0.3.3 lib/esse/search/response.rb
esse-0.3.2 lib/esse/search/response.rb
esse-0.3.1 lib/esse/search/response.rb
esse-0.3.0 lib/esse/search/response.rb
esse-0.2.6 lib/esse/search/response.rb
esse-0.2.5 lib/esse/search/response.rb
esse-0.2.4 lib/esse/search/response.rb
esse-0.2.3 lib/esse/search/response.rb
esse-0.2.2 lib/esse/search/response.rb