Module: Flex::Scope::QueryMethods
- Included in:
- Flex::Scope
- Defined in:
- lib/flex/scope/query_methods.rb
Instance Method Summary collapse
-
#all(*vars) ⇒ Object
will retrieve all documents, the results will be limited by the default :size param use #scan_all if you want to really retrieve all documents (in batches).
-
#count(*vars) ⇒ Object
performs a count search on the scope you can pass a template name as the first arg and it will be used to compute the count.
- #delete(*vars) ⇒ Object
- #each(*vars, &block) ⇒ Object
-
#find(ids, *vars) ⇒ Object
MyModel.find(ids, *vars) - ids can be a single id or an array of ids.
-
#first(*vars) ⇒ Object
it limits the size of the query to the first document and returns it as a single document object.
-
#last(*vars) ⇒ Object
it limits the size of the query to the last document and returns it as a single document object.
-
#scan_all(*vars, &block) ⇒ Object
(also: #each_batch, #find_in_batches)
scan_search: the block will be yielded many times with an array of batched results.
Instance Method Details
#all(*vars) ⇒ Object
will retrieve all documents, the results will be limited by the default :size param use #scan_all if you want to really retrieve all documents (in batches)
47 48 49 50 |
# File 'lib/flex/scope/query_methods.rb', line 47 def all(*vars) result = Query.get self, *vars result.get_docs end |
#count(*vars) ⇒ Object
performs a count search on the scope you can pass a template name as the first arg and it will be used to compute the count. For example: SearchClass.scoped.count(:search_template, vars)
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/flex/scope/query_methods.rb', line 76 def count(*vars) result = if vars.first.is_a?(Symbol) template = vars.shift # preserves an eventual wrapper by calling the template method self[:context].send(template, params(:search_type => 'count'), *vars) else Query.flex.count_search(:get, self, *vars) end result['hits']['total'] end |
#delete(*vars) ⇒ Object
67 68 69 |
# File 'lib/flex/scope/query_methods.rb', line 67 def delete(*vars) Query.delete self, *vars end |
#each(*vars, &block) ⇒ Object
52 53 54 |
# File 'lib/flex/scope/query_methods.rb', line 52 def each(*vars, &block) all(*vars).each &block end |
#find(ids, *vars) ⇒ Object
MyModel.find(ids, *vars)
- ids can be a single id or an array of ids
MyModel.find '1Momf4s0QViv-yc7wjaDCA'
#=> #<MyModel ... color: "red", size: "small">
MyModel.find ['1Momf4s0QViv-yc7wjaDCA', 'BFdIETdNQv-CuCxG_y2r8g']
#=> [#<MyModel ... color: "red", size: "small">, #<MyModel ... color: "bue", size: "small">]
22 23 24 25 26 27 28 29 |
# File 'lib/flex/scope/query_methods.rb', line 22 def find(ids, *vars) raise ArgumentError, "Empty argument passed (got #{ids.inspect})" \ if ids.nil? || ids.respond_to?(:empty?) && ids.empty? wrapped = ids.is_a?(::Array) ? ids : [ids] result = Query.ids self, *vars, :ids => wrapped docs = result.get_docs ids.is_a?(::Array) ? docs : docs.first end |
#first(*vars) ⇒ Object
it limits the size of the query to the first document and returns it as a single document object
32 33 34 35 36 |
# File 'lib/flex/scope/query_methods.rb', line 32 def first(*vars) result = Query.get params(:size => 1), *vars docs = result.get_docs docs.is_a?(Array) ? docs.first : docs end |
#last(*vars) ⇒ Object
it limits the size of the query to the last document and returns it as a single document object
39 40 41 42 43 |
# File 'lib/flex/scope/query_methods.rb', line 39 def last(*vars) result = Query.get params(:from => count-1, :size => 1), *vars docs = result.get_docs docs.is_a?(Array) ? docs.first : docs end |
#scan_all(*vars, &block) ⇒ Object Also known as: each_batch, find_in_batches
scan_search: the block will be yielded many times with an array of batched results. You can pass :scroll and :size as params in order to control the action. See www.elasticsearch.org/guide/reference/api/search/scroll.html
59 60 61 62 63 |
# File 'lib/flex/scope/query_methods.rb', line 59 def scan_all(*vars, &block) Query.flex.scan_search(:get, self, *vars) do |result| block.call result.get_docs end end |