Module: ActsAsFerret::SharedIndexClassMethods

Defined in:
lib/shared_index_class_methods.rb

Overview

class methods for classes using acts_as_ferret :single_index => true

Instance Method Summary collapse

Instance Method Details

#find_by_contents(q, options = {}, find_options = {}) ⇒ Object

Overrides the standard find_by_contents for searching a shared index.

please note that records from different models will be fetched in separate sql calls, so any sql order_by clause given with find_options will be ignored.



38
39
40
41
42
43
44
45
46
# File 'lib/shared_index_class_methods.rb', line 38

def find_by_contents(q, options = {}, find_options = {})
  if order = find_options.delete(:order)
    logger.warn "using a shared index, so ignoring order_by clause #{order}"
  end
  total_hits, result = find_records_lazy_or_not q, options, find_options
  # sort so results have the same order they had when originally retrieved
  # from ferret
  return SearchResults.new(result, total_hits)
end

#find_id_by_contents(q, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shared_index_class_methods.rb', line 6

def find_id_by_contents(q, options = {}, &block)
  # add class name scoping to query if necessary
  unless options[:models] == :all # search needs to be restricted by one or more class names
    options[:models] ||= [] 
    # add this class to the list of given models
    options[:models] << self unless options[:models].include?(self)
    # keep original query 
    original_query = q
    
    if original_query.is_a? String
      model_query = options[:models].map(&:name).join '|'
      q += %{ +class_name:"#{model_query}"}
    else
      q = Ferret::Search::BooleanQuery.new
      q.add_query(original_query, :must)
      model_query = Ferret::Search::BooleanQuery.new
      options[:models].each do |model|
        model_query.add_query(Ferret::Search::TermQuery.new(:class_name, model.name), :should)
      end
      q.add_query(model_query, :must)
    end
  end
  options.delete :models
  
  super(q, options, &block)
end