Module: Slingshot::Model::Search::ClassMethods

Included in:
Slingshot::Model::Search
Defined in:
lib/slingshot/model/search.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



54
55
56
# File 'lib/slingshot/model/search.rb', line 54

def index
  @index = Index.new(index_name)
end

#search(query = nil, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/slingshot/model/search.rb', line 28

def search(query=nil, options={}, &block)
  old_wrapper = Slingshot::Configuration.wrapper
  Slingshot::Configuration.wrapper self
  sort  = options[:order] || options[:sort]
  sort  = Array(sort)
  unless block_given?
    s = Slingshot::Search::Search.new(index.name, options)
    s.query { string query }
    s.sort do
      sort.each do |t|
        field_name, direction = t.split(' ')
        field_name.include?('.') ? field(field_name, direction) : send(field_name, direction)
      end
    end unless sort.empty?
    s.size( options[:per_page].to_i ) if options[:per_page]
    s.from( options[:page].to_i <= 1 ? 0 : (options[:per_page].to_i * (options[:page].to_i-1)) ) if options[:page] && options[:per_page]
    s.perform.results
  else
    s = Slingshot::Search::Search.new(index.name, options)
    block.arity < 1 ? s.instance_eval(&block) : block.call(s)
    s.perform.results
  end
ensure
  Slingshot::Configuration.wrapper old_wrapper
end