Class: ThinkingSphinx::Search

Inherits:
Array
  • 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?
respond_to_missing? 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.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/thinking_sphinx/search.rb', line 86

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/thinking_sphinx/search.rb', line 166

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[/^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

.bundle_searches(enum = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/thinking_sphinx/search.rb', line 64

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) ⇒ Object

Deprecated. Use ThinkingSphinx.count



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

def self.count(*args)
  warn '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)
  warn 'ThinkingSphinx::Search.facets is deprecated. Please use ThinkingSphinx.facets instead.'
  ThinkingSphinx.facets(*args)
end

.matching_fields(fields, bitmask) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/thinking_sphinx/search.rb', line 76

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)
  warn '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)
  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) ⇒ 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)
  warn 'ThinkingSphinx::Search.search_for_ids is deprecated. Please use ThinkingSphinx.search_for_ids instead.'
  ThinkingSphinx.search_for_ids(*args)
end

.warn(message) ⇒ Object



60
61
62
# File 'lib/thinking_sphinx/search.rb', line 60

def self.warn(message)
  ::ActiveSupport::Deprecation.warn message
end

Instance Method Details

#append_to(client) ⇒ Object



370
371
372
373
374
# File 'lib/thinking_sphinx/search.rb', line 370

def append_to(client)
  prepare client
  client.append_query query, indexes, comment
  client.reset
end

#as_json(*args) ⇒ Object



109
110
111
112
# File 'lib/thinking_sphinx/search.rb', line 109

def as_json(*args)
  populate
  @array.as_json(*args)
end

#clientObject



364
365
366
367
368
# File 'lib/thinking_sphinx/search.rb', line 364

def client
  client = options[:client] || config.client

  prepare client
end

#current_pageInteger

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

Returns:

  • (Integer)


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

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



302
303
304
305
306
307
308
309
# File 'lib/thinking_sphinx/search.rb', line 302

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_match(&block) ⇒ Object



319
320
321
322
323
324
# File 'lib/thinking_sphinx/search.rb', line 319

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

#each_with_weighting(&block) ⇒ Object



312
313
314
315
316
317
# File 'lib/thinking_sphinx/search.rb', line 312

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

#errorString?

The Sphinx-reported error, if any.

Returns:



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

def error
  populate
  @results[:error]
end

#error?Boolean

Indication of whether the request resulted in an error from Sphinx.

Returns:

  • (Boolean)

    true if Sphinx reports query error



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

def error?
  !!error
end

#excerpt_for(string, model = nil) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/thinking_sphinx/search.rb', line 326

def excerpt_for(string, model = nil)
  if model.nil? && one_class
    model ||= one_class
  end

  populate
  client.excerpts(
    {
      :docs   => [string.to_s],
      :words  => results[:words].keys.join(' '),
      :index  => options[:index] || "#{model.core_index_names.first}"
    }.merge(options[:excerpt_options] || {})
  ).first
end

#facets(*args) ⇒ Object



356
357
358
359
360
361
362
# File 'lib/thinking_sphinx/search.rb', line 356

def facets(*args)
  options = args.extract_options!
  merge_search self, args, options
  args << options

  ThinkingSphinx::FacetSearch.new(*args)
end

#freezeObject



103
104
105
106
107
# File 'lib/thinking_sphinx/search.rb', line 103

def freeze
  populate
  @array.freeze
  self
end

#indexesObject



293
294
295
296
297
298
299
300
# File 'lib/thinking_sphinx/search.rb', line 293

def indexes
  return options[:index] if options[:index]
  return '*' if classes.empty?

  classes.collect { |klass|
    klass.sphinx_index_names
  }.flatten.uniq.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)


216
217
218
# File 'lib/thinking_sphinx/search.rb', line 216

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)


289
290
291
# File 'lib/thinking_sphinx/search.rb', line 289

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

#page(page_number) ⇒ Object

Kaminari support



206
207
208
209
# File 'lib/thinking_sphinx/search.rb', line 206

def page(page_number)
  @options[:page] = page_number
  self
end

#per(limit) ⇒ Object

Kaminari support



243
244
245
246
# File 'lib/thinking_sphinx/search.rb', line 243

def per(limit)
  @options[:limit] = limit
  self
end

#per_pageInteger Also known as: limit_value

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

Returns:

  • (Integer)


234
235
236
237
238
# File 'lib/thinking_sphinx/search.rb', line 234

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

#populate_from_queue(results) ⇒ Object



376
377
378
379
380
381
382
# File 'lib/thinking_sphinx/search.rb', line 376

def populate_from_queue(results)
  return if @populated
  @populated = true
  @results   = results

  compose_results
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.



119
120
121
# File 'lib/thinking_sphinx/search.rb', line 119

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)


225
226
227
# File 'lib/thinking_sphinx/search.rb', line 225

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

#query_timeInteger

Query time taken

Returns:

  • (Integer)


266
267
268
269
270
271
# File 'lib/thinking_sphinx/search.rb', line 266

def query_time
  populate
  return 0 if @results[:time].nil?

  @query_time ||= @results[:time]
end

#respond_to?(method, include_private = false) ⇒ 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.



192
193
194
# File 'lib/thinking_sphinx/search.rb', line 192

def respond_to?(method, include_private = false)
  super || @array.respond_to?(method, include_private)
end

#resultsHash

The query result hash from Riddle.

Returns:

  • (Hash)

    Raw Sphinx results



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

def results
  populate
  @results
end

#search(*args) ⇒ Object



341
342
343
344
345
# File 'lib/thinking_sphinx/search.rb', line 341

def search(*args)
  args << args.extract_options!.merge(:ignore_default => true)
  merge_search ThinkingSphinx::Search.new(*args), self.args, options
  self
end

#search_for_ids(*args) ⇒ Object



347
348
349
350
351
352
353
354
# File 'lib/thinking_sphinx/search.rb', line 347

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_aObject



98
99
100
101
# File 'lib/thinking_sphinx/search.rb', line 98

def to_a
  populate
  @array
end

#total_entriesInteger

The total number of search results available.

Returns:

  • (Integer)


277
278
279
280
281
282
# File 'lib/thinking_sphinx/search.rb', line 277

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

  @total_entries ||= @results[:total_found]
end

#total_pagesInteger Also known as: page_count, num_pages

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

Returns:

  • (Integer)


252
253
254
255
256
257
# File 'lib/thinking_sphinx/search.rb', line 252

def total_pages
  populate
  return 0 if @results[:total].nil?

  @total_pages ||= (@results[:total] / per_page.to_f).ceil
end

#warningString?

The Sphinx-reported warning, if any.

Returns:



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

def warning
  populate
  @results[:warning]
end

#warning?Boolean

Indication of whether the request resulted in a warning from Sphinx.

Returns:

  • (Boolean)

    true if Sphinx reports query warning



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

def warning?
  !!warning
end