Module | ActsAsSphinx::SphinxClassMethods |
In: |
plugins/can_has_sphinx/lib/acts_as_sphinx.rb
|
VALID_OPTIONS | = | %w[mode offset page limit index weights host port range filter filter_range group_by sort_mode].map(&:to_sym) |
Performs a sphinx search and returns a hash object as defined by Sphinx#query method. This methods accepts the same set of options as :sphinx option of find_with_sphinx method.
# File plugins/can_has_sphinx/lib/acts_as_sphinx.rb, line 38 38: def ask_sphinx(query, options = {}) 39: options.assert_valid_keys(VALID_OPTIONS) 40: 41: default_options = {:offset => 0, :limit => 20} 42: default_options.merge! sphinx_options 43: options.reverse_merge! default_options 44: 45: if options[:page] && options[:limit] 46: options[:offset] = options[:limit] * (options[:page].to_i - 1) 47: options[:offset] = 0 if options[:offset] < 0 48: end 49: 50: sphinx = Sphinx.new 51: sphinx.set_server options[:host], options[:port] 52: sphinx.set_limits options[:offset], options[:limit] 53: sphinx.set_weights options[:weights] if options[:weights] 54: sphinx.set_id_range options[:range] if options[:range] 55: 56: options[:filter].each do |attr, values| 57: sphinx.set_filter attr, [*values] 58: end if options[:filter] 59: 60: options[:filter_range].each do |attr, (min, max)| 61: sphinx.set_filter_range attr, min, max 62: end if options[:filter_range] 63: 64: options[:group_by].each do |attr, func| 65: funcion = Sphinx.const_get("SPH_GROUPBY_#{func.to_s.upcase}") \ 66: rescue raise("Unknown group by function #{func}") 67: sphinx.set_group_by attr, funcion 68: end if options[:group_by] 69: 70: if options[:mode] 71: match_mode = Sphinx.const_get("SPH_MATCH_#{options[:mode].to_s.upcase}") \ 72: rescue raise("Unknown search mode #{options[:mode]}") 73: sphinx.set_match_mode match_mode 74: end 75: 76: if options[:sort_mode] 77: sort_mode, sort_expr = options[:sort_mode] 78: sort_mode = Sphinx.const_get("SPH_SORT_#{sort_mode.to_s.upcase}") \ 79: rescue raise("Unknown sort mode #{sort_mode}") 80: sphinx.set_sort_mode sort_mode, sort_expr 81: end 82: 83: sphinx.query query, options[:index] 84: end
Find all model objects using sphinx index. Besides regular ActiveRecord::Base#find method‘s options, you can specify :sphinx key that points to a hash with the following sphinx specific parameters:
:mode defines the search mode (:all, :any, :boolean, :extended) :sort_mode defines the sort mode (:relevance, :attr_desc, :attr_asc, :time_segments, :extended),
for example :sort_mode => [:attr_desc, 'myattr']
:limit restricts result to a specified number of objects, default is 20 :offset make this method return from a specific offset, default is 0 :page can be used instead of :offset option to specify the page number :host overrides the default value of this option, see acts_as_sphinx method :port overrides the default value of this option, see acts_as_sphinx method :index overrides the default index name :weight is an array of weights for each index component (used in the relevance algorithm) :range is an array that defines the range document ids to be used, e.g. :range => [min, max] :fiter and :filter_range
options define a search filter by an attribute
:group_by makes the search result to be grouped by an attribute, e.g. :group_by => [attr, function],
where function is :day, :week, :month, :year, or :attr
The returned array has three special attributes:
ary.total returns a total hits retrieved for this search ary.total_found returns a total number of hits found while scanning indexes. ary.time returns a time spent performing the search.
# File plugins/can_has_sphinx/lib/acts_as_sphinx.rb, line 111 111: def find_with_sphinx(query, options = {}) 112: result = ask_sphinx(query, options.delete(:sphinx) || {}) 113: records = result[:matches].empty? ? [] : find(result[:matches].keys, options) 114: records = records.sort_by{|r| -result[:matches][r.id][:weight] } 115: %w[total total_found time].map(&:to_sym).each do |method| 116: class << records; self end.send(:define_method, method) {result[method]} 117: end 118: records 119: end
# File plugins/can_has_sphinx/lib/acts_as_sphinx.rb, line 28 28: def sphinx_index 29: read_inheritable_attribute('sphinx_options')[:index] 30: end