Class: ThinkingSphinx::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/thinking_sphinx/search.rb

Overview

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.

Constant Summary collapse

CoreMethods =
%w( == class class_eval extend frozen? id instance_eval
instance_of? instance_values instance_variable_defined?
instance_variable_get instance_variable_set instance_variables is_a?
kind_of? member? method methods nil? object_id respond_to? send should
type )
SafeMethods =
%w( partition private_methods protected_methods
public_methods send class )
HashOptions =
[:conditions, :with, :without, :with_all]
ArrayOptions =
[:classes, :without_ids]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Search

Returns a new instance of Search.



70
71
72
73
74
75
76
# File 'lib/thinking_sphinx/search.rb', line 70

def initialize(*args)
  ThinkingSphinx.context.define_indexes
  
  @array    = []
  @options  = args.extract_options!
  @args     = args
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/thinking_sphinx/search.rb', line 101

def method_missing(method, *args, &block)
  if is_scope?(method)
    add_scope(method, *args, &block)
    return self
  elsif method == :search_count
    return scoped_count
  elsif method.to_s[/^each_with_.*/].nil? && !@array.respond_to?(method)
    super
  elsif !SafeMethods.include?(method.to_s)
    populate
  end
  
  if method.to_s[/^each_with_.*/] && !@array.respond_to?(method)
    each_with_attribute method.to_s.gsub(/^each_with_/, ''), &block
  else
    @array.send(method, *args, &block)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



28
29
30
# File 'lib/thinking_sphinx/search.rb', line 28

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/thinking_sphinx/search.rb', line 28

def options
  @options
end

Class Method Details

.count(*args) ⇒ Object

Deprecated. Use ThinkingSphinx.count



49
50
51
52
# File 'lib/thinking_sphinx/search.rb', line 49

def self.count(*args)
  log 'ThinkingSphinx::Search.count is deprecated. Please use ThinkingSphinx.count instead.'
  ThinkingSphinx.count *args
end

.facets(*args) ⇒ Object

Deprecated. Use ThinkingSphinx.facets



55
56
57
58
# File 'lib/thinking_sphinx/search.rb', line 55

def self.facets(*args)
  log 'ThinkingSphinx::Search.facets is deprecated. Please use ThinkingSphinx.facets instead.'
  ThinkingSphinx.facets *args
end

.matching_fields(fields, bitmask) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/thinking_sphinx/search.rb', line 60

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

.search(*args) ⇒ Object

Deprecated. Use ThinkingSphinx.search



31
32
33
34
# File 'lib/thinking_sphinx/search.rb', line 31

def self.search(*args)
  log 'ThinkingSphinx::Search.search is deprecated. Please use ThinkingSphinx.search instead.'
  ThinkingSphinx.search *args
end

.search_for_id(*args) ⇒ Object

Deprecated. Use ThinkingSphinx.search_for_ids



43
44
45
46
# File 'lib/thinking_sphinx/search.rb', line 43

def self.search_for_id(*args)
  log 'ThinkingSphinx::Search.search_for_id is deprecated. Please use ThinkingSphinx.search_for_id instead.'
  ThinkingSphinx.search_for_id *args
end

.search_for_ids(*args) ⇒ Object

Deprecated. Use ThinkingSphinx.search_for_ids



37
38
39
40
# File 'lib/thinking_sphinx/search.rb', line 37

def self.search_for_ids(*args)
  log 'ThinkingSphinx::Search.search_for_ids is deprecated. Please use ThinkingSphinx.search_for_ids instead.'
  ThinkingSphinx.search_for_ids *args
end

Instance Method Details

#current_pageInteger

The current page number of the result set. Defaults to 1 if no page was explicitly requested.

Returns:

  • (Integer)


135
136
137
# File 'lib/thinking_sphinx/search.rb', line 135

def current_page
  @options[:page].blank? ? 1 : @options[:page].to_i
end

#each_with_groupby_and_count(&block) ⇒ Object Also known as: each_with_group_and_count



208
209
210
211
212
213
214
215
# File 'lib/thinking_sphinx/search.rb', line 208

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

#each_with_weighting(&block) ⇒ Object



218
219
220
221
222
223
# File 'lib/thinking_sphinx/search.rb', line 218

def each_with_weighting(&block)
  populate
  results[:matches].each_with_index do |match, index|
    yield self[index], match[:weight]
  end
end

#excerpt_for(string, model = nil) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/thinking_sphinx/search.rb', line 225

def excerpt_for(string, model = nil)
  if model.nil? && one_class
    model ||= one_class
  end
  
  populate
  client.excerpts(
    :docs   => [string],
    :words  => results[:words].keys.join(' '),
    :index  => "#{model.source_of_sphinx_index.sphinx_name}_core"
  ).first
end

#indexesObject



201
202
203
204
205
206
# File 'lib/thinking_sphinx/search.rb', line 201

def indexes
  return options[:index] if options[:index]
  return '*' if classes.empty?
  
  classes.collect { |klass| klass.sphinx_index_names }.flatten.join(',')
end

#next_pageInteger?

The next page number of the result set. If there are no more pages available, nil is returned.

Returns:

  • (Integer, nil)


144
145
146
# File 'lib/thinking_sphinx/search.rb', line 144

def next_page
  current_page >= total_pages ? nil : current_page + 1
end

#offsetInteger

The current page’s offset, based on the number of records per page. Or explicit :offset if given.

Returns:

  • (Integer)


197
198
199
# File 'lib/thinking_sphinx/search.rb', line 197

def offset
  @options[:offset] || ((current_page - 1) * per_page)
end

#per_pageInteger

The amount of records per set of paged results. Defaults to 20 unless a specific page size is requested.

Returns:

  • (Integer)


162
163
164
165
166
# File 'lib/thinking_sphinx/search.rb', line 162

def per_page
  @options[:limit] ||= @options[:per_page]
  @options[:limit] ||= 20
  @options[:limit].to_i
end

#populated?Boolean

Indication of whether the request has been made to Sphinx for the search query.

Returns:

  • (Boolean)

    true if the results have been requested.



88
89
90
# File 'lib/thinking_sphinx/search.rb', line 88

def populated?
  !!@populated
end

#previous_pageInteger?

The previous page number of the result set. If this is the first page, then nil is returned.

Returns:

  • (Integer, nil)


153
154
155
# File 'lib/thinking_sphinx/search.rb', line 153

def previous_page
  current_page == 1 ? nil : current_page - 1
end

#respond_to?(method) ⇒ Boolean

Returns true if the Search object or the underlying Array object respond to the requested method.

Parameters:

  • method (Symbol)

    The method name

Returns:

  • (Boolean)

    true if either Search or Array responds to the method.



126
127
128
# File 'lib/thinking_sphinx/search.rb', line 126

def respond_to?(method)
  super || @array.respond_to?(method)
end

#resultsHash

The query result hash from Riddle.

Returns:

  • (Hash)

    Raw Sphinx results



96
97
98
99
# File 'lib/thinking_sphinx/search.rb', line 96

def results
  populate
  @results
end

#search(*args) ⇒ Object



238
239
240
241
242
# File 'lib/thinking_sphinx/search.rb', line 238

def search(*args)
  add_default_scope
  merge_search ThinkingSphinx::Search.new(*args)
  self
end

#to_aObject



78
79
80
81
# File 'lib/thinking_sphinx/search.rb', line 78

def to_a
  populate
  @array
end

#total_entriesInteger

The total number of search results available.

Returns:

  • (Integer)


185
186
187
188
189
190
# File 'lib/thinking_sphinx/search.rb', line 185

def total_entries
  populate
  return 0 if @results[:total_found].nil?
  
  @total_entries ||= @results[:total_found]
end

#total_pagesInteger Also known as: page_count

The total number of pages available if the results are paginated.

Returns:

  • (Integer)


172
173
174
175
176
177
# File 'lib/thinking_sphinx/search.rb', line 172

def total_pages
  populate
  return 0 if @results[:total].nil?
  
  @total_pages ||= (@results[:total] / per_page.to_f).ceil
end