Once you’ve got those indexes in and built, this is the stuff that matters - how to search! This class provides a generic search interface - which you can use to search all your indexed models at once. Most times, you will just want a specific model’s results - to search and ::search_for_ids methods will do the job in exactly the same manner when called from a model.
# File lib/thinking_sphinx/search.rb, line 63 def self.bundle_searches(enum = nil) bundle = ThinkingSphinx::BundledSearch.new if enum.nil? yield bundle else enum.each { |item| yield bundle, item } end bundle.searches end
Deprecated. Use ThinkingSphinx.count
# File lib/thinking_sphinx/search.rb, line 48 def self.count(*args) warn 'ThinkingSphinx::Search.count is deprecated. Please use ThinkingSphinx.count instead.' ThinkingSphinx.count(*args) end
Deprecated. Use ThinkingSphinx.facets
# File lib/thinking_sphinx/search.rb, line 54 def self.facets(*args) warn 'ThinkingSphinx::Search.facets is deprecated. Please use ThinkingSphinx.facets instead.' ThinkingSphinx.facets(*args) end
# File lib/thinking_sphinx/search.rb, line 75 def self.matching_fields(fields, bitmask) matches = [] bitstring = bitmask.to_s(2).rjust(32, '0').reverse fields.each_with_index do |field, index| matches << field if bitstring[index, 1] == '1' end matches end
# File lib/thinking_sphinx/search.rb, line 85 def initialize(*args) ThinkingSphinx.context.define_indexes @array = [] @options = args.extract_options! @args = args add_default_scope unless options[:ignore_default] populate if @options[:populate] end
Deprecated. Use ThinkingSphinx.search
# File lib/thinking_sphinx/search.rb, line 30 def self.search(*args) warn 'ThinkingSphinx::Search.search is deprecated. Please use ThinkingSphinx.search instead.' ThinkingSphinx.search(*args) end
Deprecated. Use ThinkingSphinx.search_for_ids
# File lib/thinking_sphinx/search.rb, line 42 def self.search_for_id(*args) warn 'ThinkingSphinx::Search.search_for_id is deprecated. Please use ThinkingSphinx.search_for_id instead.' ThinkingSphinx.search_for_id(*args) end
Deprecated. Use ThinkingSphinx.search_for_ids
# File lib/thinking_sphinx/search.rb, line 36 def self.search_for_ids(*args) warn 'ThinkingSphinx::Search.search_for_ids is deprecated. Please use ThinkingSphinx.search_for_ids instead.' ThinkingSphinx.search_for_ids(*args) end
# File lib/thinking_sphinx/search.rb, line 59 def self.warn(message) ::ActiveSupport::Deprecation.warn message end
# File lib/thinking_sphinx/search.rb, line 97 def ==(object) populate super end
Populates the search result set
# File lib/thinking_sphinx/search.rb, line 108 def all populate self end
# File lib/thinking_sphinx/search.rb, line 399 def append_to(client) prepare client client.append_query query, indexes, comment client.reset end
# File lib/thinking_sphinx/search.rb, line 119 def as_json(*args) populate @array.as_json(*args) end
# File lib/thinking_sphinx/search.rb, line 393 def client client = options[:client] || config.client prepare client end
The current page number of the result set. Defaults to 1 if no page was explicitly requested.
@return [Integer]
# File lib/thinking_sphinx/search.rb, line 211 def current_page @options[:page].blank? ? 1 : @options[:page].to_i end
# File lib/thinking_sphinx/search.rb, line 329 def each_with_groupby_and_count(&block) populate results[:matches].each_with_index do |match, index| yield self[index], match[:attributes]["@groupby"], match[:attributes]["@count"] end end
# File lib/thinking_sphinx/search.rb, line 346 def each_with_match(&block) populate results[:matches].each_with_index do |match, index| yield self[index], match end end
# File lib/thinking_sphinx/search.rb, line 339 def each_with_weighting(&block) populate results[:matches].each_with_index do |match, index| yield self[index], match[:weight] end end
The Sphinx-reported error, if any.
@return [String, nil]
# File lib/thinking_sphinx/search.rb, line 145 def error populate @results[:error] end
Indication of whether the request resulted in an error from Sphinx.
@return [Boolean] true if Sphinx reports query error
# File lib/thinking_sphinx/search.rb, line 137 def error? !!error end
# File lib/thinking_sphinx/search.rb, line 353 def excerpt_for(string, model = nil) if model.nil? && one_class model ||= one_class end populate index = options[:index] || "#{model.core_index_names.first}" client.excerpts( { :docs => [string.to_s], :words => results[:words].keys.join(' '), :index => index.split(',').first.strip }.merge(options[:excerpt_options] || {}) ).first end
# File lib/thinking_sphinx/search.rb, line 385 def facets(*args) options = args.extract_options! merge_search self, args, options args << options ThinkingSphinx::FacetSearch.new(*args) end
# File lib/thinking_sphinx/search.rb, line 215 def first_page? current_page == 1 end
# File lib/thinking_sphinx/search.rb, line 113 def freeze populate @array.freeze self end
# File lib/thinking_sphinx/search.rb, line 320 def indexes return options[:index] if options[:index] return '*' if classes.empty? classes.collect { |klass| klass.sphinx_index_names }.flatten.uniq.join(',') end
# File lib/thinking_sphinx/search.rb, line 238 def last_page? next_page.nil? end
# File lib/thinking_sphinx/search.rb, line 176 def method_missing(method, *args, &block) if is_scope?(method) add_scope(method, *args, &block) return self elsif method == :search_count merge_search one_class.search(*args), self.args, options return scoped_count elsif method.to_s[%r^each_with_.*/].nil? && !@array.respond_to?(method) super elsif !SafeMethods.include?(method.to_s) populate end if method.to_s[%r^each_with_.*/] && !@array.respond_to?(method) each_with_attribute method.to_s.gsub(%r^each_with_/, ''), &block else @array.send(method, *args, &block) end end
The next page number of the result set. If there are no more pages available, nil is returned.
@return [Integer, nil]
# File lib/thinking_sphinx/search.rb, line 230 def next_page current_page >= total_pages ? nil : current_page + 1 end
# File lib/thinking_sphinx/search.rb, line 234 def next_page? !next_page.nil? end
The current page’s offset, based on the number of records per page. Or explicit :offset if given.
@return [Integer]
# File lib/thinking_sphinx/search.rb, line 314 def offset @options[:offset] || ((current_page - 1) * per_page) end
Kaminari support
# File lib/thinking_sphinx/search.rb, line 220 def page(page_number) @options[:page] = page_number self end
Compatibility with kaminari and older versions of will_paginate
Kaminari support
# File lib/thinking_sphinx/search.rb, line 265 def per(limit) @options[:limit] = limit self end
The amount of records per set of paged results. Defaults to 20 unless a specific page size is requested.
@return [Integer]
# File lib/thinking_sphinx/search.rb, line 256 def per_page @options[:limit] ||= @options[:per_page] @options[:limit] ||= 20 @options[:limit].to_i end
# File lib/thinking_sphinx/search.rb, line 405 def populate_from_queue(results) return if @populated @populated = true @results = results compose_results end
Indication of whether the request has been made to Sphinx for the search query.
@return [Boolean] true if the results have been requested.
# File lib/thinking_sphinx/search.rb, line 129 def populated? !!@populated end
The previous page number of the result set. If this is the first page, then nil is returned.
@return [Integer, nil]
# File lib/thinking_sphinx/search.rb, line 247 def previous_page current_page == 1 ? nil : current_page - 1 end
Query time taken
@return [Integer]
# File lib/thinking_sphinx/search.rb, line 288 def query_time populate return 0 if @results[:time].nil? @query_time ||= @results[:time] end
Returns true if the Search object or the underlying Array object respond to the requested method.
@param [Symbol] method The method name @return [Boolean] true if either Search or Array responds to the method.
# File lib/thinking_sphinx/search.rb, line 202 def respond_to?(method, include_private = false) super || @array.respond_to?(method, include_private) end
The query result hash from Riddle.
@return [Hash] Raw Sphinx results
# File lib/thinking_sphinx/search.rb, line 171 def results populate @results end
# File lib/thinking_sphinx/search.rb, line 370 def search(*args) args << args.extract_options!.merge(:ignore_default => true) merge_search ThinkingSphinx::Search.new(*args), self.args, options self end
# File lib/thinking_sphinx/search.rb, line 376 def search_for_ids(*args) args << args.extract_options!.merge( :ignore_default => true, :ids_only => true ) merge_search ThinkingSphinx::Search.new(*args), self.args, options self end
# File lib/thinking_sphinx/search.rb, line 102 def to_a populate @array end
The total number of search results available.
@return [Integer]
# File lib/thinking_sphinx/search.rb, line 299 def total_entries populate return 0 if @results.nil? || @results[:total_found].nil? @total_entries ||= @results[:total_found] end
The total number of pages available if the results are paginated.
@return [Integer]
# File lib/thinking_sphinx/search.rb, line 274 def total_pages populate return 0 if @results.nil? || @results[:total].nil? @total_pages ||= (@results[:total] / per_page.to_f).ceil end
The Sphinx-reported warning, if any.
@return [String, nil]
# File lib/thinking_sphinx/search.rb, line 162 def warning populate @results[:warning] end
Indication of whether the request resulted in a warning from Sphinx.
@return [Boolean] true if Sphinx reports query warning
# File lib/thinking_sphinx/search.rb, line 154 def warning? !!warning end