Class: JayAPI::Elasticsearch::QueryResults

Inherits:
Object
  • Object
show all
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

SearchAfterResults

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, query:, response:, batch_counter: nil) ⇒ QueryResults

Creates a new instance of the class.

Parameters:

  • index (JayAPI::Elasticsearch::Indexable)

    The Elasticsearch index or indexes over which the query should be performed.

  • query (Hash)

    The query that produced the results.

  • response (JayAPI::Elasticsearch::Results)

    An object containing Docs retrieved from Elasticsearch.

  • batch_counter (JayAPI::Elasticsearch::BatchCounter) (defaults to: nil)

    An object keeping track of the current batch.



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_counterObject (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

#indexObject (readonly)

Returns the value of attribute index.



18
19
20
# File 'lib/jay_api/elasticsearch/query_results.rb', line 18

def index
  @index
end

#queryObject (readonly)

Returns the value of attribute query.



18
19
20
# File 'lib/jay_api/elasticsearch/query_results.rb', line 18

def query
  @query
end

#responseObject (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.

Yields:

  • (Hash)

    Each document in the current QueryResults object.

Returns:

  • (JayAPI::Elasticsearch::QueryResults, Enumerator)

    If a block is given the object with the last batch of documents (can be the receiver if there is only one batch) will be returned. If no block is given an Enumerator will be returned.



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.

Yields:

  • (Hash)

    Each document in the current QueryResults object.

Returns:

  • (Enumerator, Array)

    An enumerator with all the objects in the QueryResults object if no block is given, or an array of all the documents in the QueryResults object.



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.

Returns:

  • (Boolean)

    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_batchJayAPI::Elasticsearch::QueryResults

Fetches the next batch of documents.

Returns:

Raises:

  • (Elasticsearch::Transport::Transport::ServerError)

    If the query fails.



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