class ThinkingSphinx::Search

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.

Constants

ArrayOptions
CoreMethods
DefaultStarToken
HashOptions
SafeMethods

Attributes

args[R]
options[R]

Public Class Methods

bundle_searches(enum = nil) { |bundle| ... } click to toggle source
# 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
count(*args) click to toggle source

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
facets(*args) click to toggle source

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
matching_fields(fields, bitmask) click to toggle source
# 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
new(*args) click to toggle source
# 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
search_for_id(*args) click to toggle source

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
search_for_ids(*args) click to toggle source

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
warn(message) click to toggle source
# File lib/thinking_sphinx/search.rb, line 59
def self.warn(message)
  ::ActiveSupport::Deprecation.warn message
end

Public Instance Methods

==(object) click to toggle source
# File lib/thinking_sphinx/search.rb, line 97
def ==(object)
  populate
  super
end
all() click to toggle source

Populates the search result set

# File lib/thinking_sphinx/search.rb, line 108
def all
  populate
  self
end
append_to(client) click to toggle source
# File lib/thinking_sphinx/search.rb, line 399
def append_to(client)
  prepare client
  client.append_query query, indexes, comment
  client.reset
end
as_json(*args) click to toggle source
# File lib/thinking_sphinx/search.rb, line 119
def as_json(*args)
  populate
  @array.as_json(*args)
end
client() click to toggle source
# File lib/thinking_sphinx/search.rb, line 393
def client
  client = options[:client] || config.client

  prepare client
end
current_page() click to toggle source

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
each_with_group_and_count(&block) click to toggle source
each_with_groupby_and_count() { |self, match["groupby"], match["count"]| ... } click to toggle source
# 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
Also aliased as: each_with_group_and_count
each_with_match() { |self, match| ... } click to toggle source
# 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
each_with_weighting() { |self, match| ... } click to toggle source
# 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
error() click to toggle source

The Sphinx-reported error, if any.

@return [String, nil]

# File lib/thinking_sphinx/search.rb, line 145
def error
  populate
  @results[:error]
end
error?() click to toggle source

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
excerpt_for(string, model = nil) click to toggle source
# 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
facets(*args) click to toggle source
# 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
first_page?() click to toggle source
# File lib/thinking_sphinx/search.rb, line 215
def first_page?
  current_page == 1
end
freeze() click to toggle source
# File lib/thinking_sphinx/search.rb, line 113
def freeze
  populate
  @array.freeze
  self
end
indexes() click to toggle source
# 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
last_page?() click to toggle source
# File lib/thinking_sphinx/search.rb, line 238
def last_page?
  next_page.nil?
end
limit_value() click to toggle source

Kaminari support

Alias for: per_page
method_missing(method, *args, &block) click to toggle source
# 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
next_page() click to toggle source

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
next_page?() click to toggle source
# File lib/thinking_sphinx/search.rb, line 234
def next_page?
  !next_page.nil?
end
num_pages() click to toggle source
Alias for: total_pages
offset() click to toggle source

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
Also aliased as: offset_value
offset_value() click to toggle source
Alias for: offset
page(page_number) click to toggle source

Kaminari support

# File lib/thinking_sphinx/search.rb, line 220
def page(page_number)
  @options[:page] = page_number
  self
end
page_count() click to toggle source

Compatibility with kaminari and older versions of will_paginate

Alias for: total_pages
per(limit) click to toggle source

Kaminari support

# File lib/thinking_sphinx/search.rb, line 265
def per(limit)
  @options[:limit] = limit
  self
end
per_page() click to toggle source

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
Also aliased as: limit_value
populate_from_queue(results) click to toggle source
# File lib/thinking_sphinx/search.rb, line 405
def populate_from_queue(results)
  return if @populated
  @populated = true
  @results   = results

  compose_results
end
populated?() click to toggle source

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
previous_page() click to toggle source

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() click to toggle source

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
respond_to?(method, include_private = false) click to toggle source

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
results() click to toggle source

The query result hash from Riddle.

@return [Hash] Raw Sphinx results

# File lib/thinking_sphinx/search.rb, line 171
def results
  populate
  @results
end
search_for_ids(*args) click to toggle source
# 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
to_a() click to toggle source
# File lib/thinking_sphinx/search.rb, line 102
def to_a
  populate
  @array
end
total_count() click to toggle source

Compatibility with kaminari

Alias for: total_entries
total_entries() click to toggle source

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
Also aliased as: total_count
total_pages() click to toggle source

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
Also aliased as: page_count, num_pages
warning() click to toggle source

The Sphinx-reported warning, if any.

@return [String, nil]

# File lib/thinking_sphinx/search.rb, line 162
def warning
  populate
  @results[:warning]
end
warning?() click to toggle source

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