Class: Supernova::ThinkingSphinxCriteria

Inherits:
Criteria
  • Object
show all
Defined in:
lib/supernova/thinking_sphinx_criteria.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.index_statement_for(field_name, column = nil) ⇒ Object



5
6
7
8
# File 'lib/supernova/thinking_sphinx_criteria.rb', line 5

def self.index_statement_for(field_name, column = nil)
  column ||= field_name
  [%(CONCAT("#{field_name}_", #{column})), { :as => :"indexed_#{field_name}" }]
end

Instance Method Details

#executeObject



37
38
39
# File 'lib/supernova/thinking_sphinx_criteria.rb', line 37

def execute
  ThinkingSphinx.search(*self.to_params)
end

#idsObject



41
42
43
44
# File 'lib/supernova/thinking_sphinx_criteria.rb', line 41

def ids
  params = *self.to_params
  ThinkingSphinx.search_for_ids(*params)
end

#normalize_with_filter(attributes) ⇒ Object



10
11
12
13
14
15
# File 'lib/supernova/thinking_sphinx_criteria.rb', line 10

def normalize_with_filter(attributes)
  attributes.inject({}) do |hash, (key, value)|
    value = Zlib.crc32(value.to_s) if value.is_a?(String) || value.is_a?(Class)
    hash.merge!(key => value)
  end
end

#to_paramsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/supernova/thinking_sphinx_criteria.rb', line 17

def to_params
  sphinx_options = { :match_mode => :boolean, :with => {}, :conditions => {}, :without => {} }
  sphinx_options[:order] = self.search_options[:order] if self.search_options[:order]
  sphinx_options[:limit] = self.search_options[:limit] if self.search_options[:limit]
  sphinx_options[:select] = self.search_options[:select] if self.search_options[:select]
  sphinx_options[:group_by] = self.search_options[:group_by] if self.search_options[:group_by]
  sphinx_options.merge!(self.search_options[:pagination]) if self.search_options[:pagination].is_a?(Hash)
  sphinx_options[:classes] = self.filters[:classes] if self.filters[:classes]
  sphinx_options[:classes] = [self.clazz] if self.clazz
  sphinx_options[:conditions].merge!(self.filters[:conditions]) if self.filters[:conditions]
  sphinx_options[:with].merge!(normalize_with_filter(self.filters[:with])) if self.filters[:with]
  sphinx_options[:without].merge!(normalize_with_filter(self.filters[:without])) if self.filters[:without]
  sphinx_options.merge!(self.search_options[:custom_options]) if self.search_options[:custom_options]
  if self.search_options[:geo_center] && self.search_options[:geo_distance]
    sphinx_options[:geo] = [self.search_options[:geo_center][:lat].to_radians, self.search_options[:geo_center][:lng].to_radians]
    sphinx_options[:with]["@geodist"] = self.search_options[:geo_distance].is_a?(Range) ? self.search_options[:geo_distance] : Range.new(0.0, self.search_options[:geo_distance])
  end
  [(self.search_options[:search] || Array.new).join(" "), sphinx_options]
end

#total_entriesObject



46
47
48
# File 'lib/supernova/thinking_sphinx_criteria.rb', line 46

def total_entries
  ids.total_entries
end