Class: ElasticGraph::GraphQL::DatastoreResponse::SearchResponse
- Inherits:
-
Object
- Object
- ElasticGraph::GraphQL::DatastoreResponse::SearchResponse
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/elastic_graph/graphql/datastore_response/search_response.rb
Overview
Represents a search response from the datastore. Exposes both the raw metadata provided by the datastore and the collection of documents. Can be treated as a collection of documents when you don’t care about the metadata.
Constant Summary collapse
- EXCLUDED_METADATA_KEYS =
%w[hits aggregations].freeze
- RAW_EMPTY =
Benign empty response that can be used in place of datastore response errors as needed.
{"hits" => {"hits" => [], "total" => {"value" => 0}}}.freeze
- EMPTY =
build(RAW_EMPTY)
Class Method Summary collapse
Instance Method Summary collapse
- #docs_description ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #total_document_count ⇒ Object
Class Method Details
.build(raw_data, decoded_cursor_factory: DecodedCursor::Factory::Null) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/elastic_graph/graphql/datastore_response/search_response.rb', line 28 def self.build(raw_data, decoded_cursor_factory: DecodedCursor::Factory::Null) documents = raw_data.fetch("hits").fetch("hits").map do |doc| Document.build(doc, decoded_cursor_factory: decoded_cursor_factory) end = raw_data.except(*EXCLUDED_METADATA_KEYS) ["hits"] = raw_data.fetch("hits").except("hits") # `hits.total` is exposed as an object like: # # { # "value" => 200, # "relation" => "eq", # or "gte" # } # # This allows it to provide a lower bound on the number of hits, rather than having # to give an exact count. We may want to handle the `gte` case differently at some # point but for now we just use the value as-is. # # In the case where `track_total_hits` flag is set to `false`, `hits.total` field will be completely absent. # This means the client intentionally chose not to query the total doc count, and `total_document_count` will be nil. # In this case, we will throw an exception if the client later tries to access `total_document_count`. total_document_count = .dig("hits", "total", "value") new( raw_data: raw_data, metadata: , documents: documents, total_document_count: total_document_count ) end |
Instance Method Details
#docs_description ⇒ Object
64 65 66 |
# File 'lib/elastic_graph/graphql/datastore_response/search_response.rb', line 64 def docs_description (documents.size < 3) ? documents.inspect : "[#{documents.first}, ..., #{documents.last}]" end |
#to_s ⇒ Object Also known as: inspect
72 73 74 |
# File 'lib/elastic_graph/graphql/datastore_response/search_response.rb', line 72 def to_s "#<#{self.class.name} size=#{documents.size} #{docs_description}>" end |
#total_document_count ⇒ Object
68 69 70 |
# File 'lib/elastic_graph/graphql/datastore_response/search_response.rb', line 68 def total_document_count super || raise(CountUnavailableError, "#{__method__} is unavailable; set `query.total_document_count_needed = true` to make it available") end |