Class: JayAPI::Elasticsearch::QueryResults
- Inherits:
-
Object
- Object
- JayAPI::Elasticsearch::QueryResults
- Extended by:
- Forwardable
- Defined in:
- lib/jay_api/elasticsearch/query_results.rb
Overview
Represents the results of an Elasticsearch query. It provides a facade in front of the returned Hash and allows more results to be fetched dynamically.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#batch_counter ⇒ Object
readonly
Returns the value of attribute batch_counter.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#all {|Hash| ... } ⇒ JayAPI::Elasticsearch::QueryResults, Enumerator
Allows the entire set of documents to be iterated in batches.
-
#each {|Hash| ... } ⇒ Enumerator, Array
Calls the given block for every document in the QueryResults object or returns an Enumerator with all the documents if no block is given.
-
#initialize(index:, query:, response:, batch_counter: nil) ⇒ QueryResults
constructor
Creates a new instance of the class.
-
#more? ⇒ Boolean
True if there are still more documents matched by the query and a call to next_batch can be performed.
-
#next_batch ⇒ JayAPI::Elasticsearch::QueryResults
Fetches the next batch of documents.
Constructor Details
#initialize(index:, query:, response:, batch_counter: nil) ⇒ QueryResults
Creates a new instance of the class.
29 30 31 32 33 34 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 29 def initialize(index:, query:, response:, batch_counter: nil) @index = index @query = query.with_indifferent_access @response = response @batch_counter = batch_counter end |
Instance Attribute Details
#batch_counter ⇒ Object (readonly)
Returns the value of attribute batch_counter.
18 19 20 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 18 def batch_counter @batch_counter end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
18 19 20 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 18 def index @index end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
18 19 20 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 18 def query @query end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
18 19 20 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 18 def response @response end |
Instance Method Details
#all {|Hash| ... } ⇒ JayAPI::Elasticsearch::QueryResults, Enumerator
Allows the entire set of documents to be iterated in batches.
- If the method is invoked with a block, the given block will be called
for every document in the +QueryResults+ object. Upon reaching the
end of the collection the next batch will be requested and the block
will be called again for each of the documents in the next batch, the
process will continue until there are no more documents. At the end,
the last batch of documents will be returned.
- If the method is called without a block an +Enumerator+ object will
be returned. Said +Enumerator+ can be used to iterate through the
whole set of documents. The +#all+ method will take care of fetching
them in batches and yielding them to the enumerator.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 71 def all(&block) return enum_for(:all) { total - start_current } unless block data = self loop do data.each(&block) break unless data.more? && data.any? data = data.next_batch end data end |
#each {|Hash| ... } ⇒ Enumerator, Array
Calls the given block for every document in the QueryResults object or returns an Enumerator with all the documents if no block is given.
48 49 50 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 48 def each(&block) hits.each(&block) end |
#more? ⇒ Boolean
Returns True if there are still more documents matched by the query and a call to next_batch can be performed.
38 39 40 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 38 def more? start_next < total end |
#next_batch ⇒ JayAPI::Elasticsearch::QueryResults
Fetches the next batch of documents.
92 93 94 95 96 97 |
# File 'lib/jay_api/elasticsearch/query_results.rb', line 92 def next_batch raise Errors::EndOfQueryResultsError unless more? modified_query = adapt_query index.search(modified_query, batch_counter: batch_counter) end |