module Flex::Scope::QueryMethods
Public Instance Methods
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)
# File lib/flex/scope/query_methods.rb, line 47 def all(*vars) result = Query.get self, *vars result.get_docs end
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)
# 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
# File lib/flex/scope/query_methods.rb, line 67 def delete(*vars) Query.delete self, *vars end
# File lib/flex/scope/query_methods.rb, line 52 def each(*vars, &block) all(*vars).each &block end
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">]
# 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
it limits the size of the query to the first document and returns it as a single document object
# 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
it limits the size of the query to the last document and returns it as a single document object
# 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_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
# 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