module Elastics::Scope::QueryMethods

Public Instance Methods

all(*vars) click to toggle source

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/elastics/scope/query_methods.rb, line 47
def all(*vars)
  result = Query.get self, *vars
  result.get_docs
end
count(*vars) click to toggle source

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/elastics/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(:size => 0), *vars)
           else
             Query.elastics.count_search(:get, self, *vars)
           end
  result['hits']['total']
end
delete(*vars) click to toggle source
# File lib/elastics/scope/query_methods.rb, line 67
def delete(*vars)
  Query.delete self, *vars
end
each(*vars, &block) click to toggle source
# File lib/elastics/scope/query_methods.rb, line 52
def each(*vars, &block)
  all(*vars).each &block
end
each_batch(*vars, &block)
Alias for: scan_all
find(ids, *vars) click to toggle source

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/elastics/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
find_in_batches(*vars, &block)
Alias for: scan_all
first(*vars) click to toggle source

it limits the size of the query to the first document and returns it as a single document object

# File lib/elastics/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) click to toggle source

it limits the size of the query to the last document and returns it as a single document object

# File lib/elastics/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) click to toggle source

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/elastics/scope/query_methods.rb, line 59
def scan_all(*vars, &block)
  Query.elastics.scan_search(:get, self, *vars) do |result|
    block.call result.get_docs
  end
end
Also aliased as: each_batch, find_in_batches