Module: Elasticsearch::Persistence::Model::Find::ClassMethods
- Defined in:
- lib/elasticsearch/persistence/model/find.rb
Instance Method Summary collapse
-
#all(query = { query: { match_all: {} } }, options = {}) ⇒ Object
Returns all models (up to 10,000).
-
#count(query_or_definition = nil, options = {}) ⇒ Integer
Returns the number of models.
-
#find_each(options = {}) ⇒ String, Enumerator
Iterate effectively over models using the ‘find_in_batches` method.
-
#find_in_batches(options = {}, &block) ⇒ String, Enumerator
Returns all models efficiently via the Elasticsearch’s scan/scroll API.
- #search(query_or_definition, options = {}) ⇒ Object
Instance Method Details
#all(query = { query: { match_all: {} } }, options = {}) ⇒ Object
Returns all models (up to 10,000)
30 31 32 33 |
# File 'lib/elasticsearch/persistence/model/find.rb', line 30 def all(query={ query: { match_all: {} } }, ={}) query[:size] ||= 10_000 search(query, ) end |
#count(query_or_definition = nil, options = {}) ⇒ Integer
Returns the number of models
54 55 56 |
# File 'lib/elasticsearch/persistence/model/find.rb', line 54 def count(query_or_definition=nil, ={}) gateway.count( query_or_definition, ) end |
#find_each(options = {}) ⇒ String, Enumerator
Iterate effectively over models using the ‘find_in_batches` method.
All the options are passed to ‘find_in_batches` and each result is yielded to the passed block.
170 171 172 173 174 175 176 |
# File 'lib/elasticsearch/persistence/model/find.rb', line 170 def find_each( = {}) return to_enum(:find_each, ) unless block_given? find_in_batches() do |batch| batch.each { |result| yield result } end end |
#find_in_batches(options = {}, &block) ⇒ String, Enumerator
Returns all models efficiently via the Elasticsearch’s scan/scroll API
You can restrict the models being returned with a query.
The Search API options are passed to the search method as parameters, all remaining options are passed as the ‘:body` parameter.
The full Repository::Response::Results instance is yielded to the passed block in each batch, so you can access any of its properties; calling ‘to_a` will convert the object to an Array of model instances.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/elasticsearch/persistence/model/find.rb', line 93 def find_in_batches(={}, &block) return to_enum(:find_in_batches, ) unless block_given? search_params = .extract!( :index, :type, :scroll, :size, :explain, :ignore_indices, :ignore_unavailable, :allow_no_indices, :expand_wildcards, :preference, :q, :routing, :source, :_source, :_source_include, :_source_exclude, :stats, :timeout) scroll = search_params.delete(:scroll) || '5m' body = # Get the initial scroll_id # response = gateway.client.search( { index: gateway.index_name, type: gateway.document_type, search_type: 'scan', scroll: scroll, size: 20, body: body }.merge(search_params) ) # Get the initial batch of documents # response = gateway.client.scroll( { scroll_id: response['_scroll_id'], scroll: scroll } ) # Break when receiving an empty array of hits # while response['hits']['hits'].any? do yield Repository::Response::Results.new(gateway, response) response = gateway.client.scroll( { scroll_id: response['_scroll_id'], scroll: scroll } ) end return response['_scroll_id'] end |
#search(query_or_definition, options = {}) ⇒ Object
14 15 16 |
# File 'lib/elasticsearch/persistence/model/find.rb', line 14 def search(query_or_definition, ={}) SearchRequest.new(self, query_or_definition, ).execute! end |