Class: ThinkingSphinx::Search
- Inherits:
-
Array
- Object
- Array
- ThinkingSphinx::Search
- 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
- 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, :without_any]
- ArrayOptions =
[:classes, :without_ids]
Instance Attribute Summary (collapse)
-
- (Object) args
readonly
Returns the value of attribute args.
-
- (Object) options
readonly
Returns the value of attribute options.
Class Method Summary (collapse)
- + (Object) bundle_searches(enum = nil)
-
+ (Object) count(*args)
Deprecated.
-
+ (Object) facets(*args)
Deprecated.
- + (Object) matching_fields(fields, bitmask)
-
+ (Object) search(*args)
Deprecated.
-
+ (Object) search_for_id(*args)
Deprecated.
-
+ (Object) search_for_ids(*args)
Deprecated.
- + (Object) warn(message)
Instance Method Summary (collapse)
- - (Object) ==(object)
-
- (Object) all
Populates the search result set.
- - (Object) append_to(client)
- - (Object) as_json(*args)
- - (Object) client
-
- (Integer) current_page
The current page number of the result set.
- - (Object) each_with_groupby_and_count(&block) (also: #each_with_group_and_count)
- - (Object) each_with_match(&block)
- - (Object) each_with_weighting(&block)
-
- (String?) error
The Sphinx-reported error, if any.
-
- (Boolean) error?
Indication of whether the request resulted in an error from Sphinx.
- - (Object) excerpt_for(string, model = nil)
- - (Object) facets(*args)
- - (Boolean) first_page?
- - (Object) freeze
- - (Object) indexes
-
- (Search) initialize(*args)
constructor
A new instance of Search.
- - (Boolean) last_page?
- - (Object) method_missing(method, *args, &block)
-
- (Integer?) next_page
The next page number of the result set.
- - (Boolean) next_page?
-
- (Integer) offset
(also: #offset_value)
The current page's offset, based on the number of records per page.
-
- (Object) page(page_number)
Kaminari support.
-
- (Object) per(limit)
Kaminari support.
-
- (Integer) per_page
(also: #limit_value)
The amount of records per set of paged results.
- - (Object) populate_from_queue(results)
-
- (Boolean) populated?
Indication of whether the request has been made to Sphinx for the search query.
-
- (Integer?) previous_page
The previous page number of the result set.
-
- (Integer) query_time
Query time taken.
-
- (Boolean) respond_to?(method, include_private = false)
Returns true if the Search object or the underlying Array object respond to the requested method.
-
- (Hash) results
The query result hash from Riddle.
- - (Object) search(*args)
- - (Object) search_for_ids(*args)
- - (Object) to_a
-
- (Integer) total_entries
(also: #total_count)
The total number of search results available.
-
- (Integer) total_pages
(also: #page_count, #num_pages)
The total number of pages available if the results are paginated.
-
- (String?) warning
The Sphinx-reported warning, if any.
-
- (Boolean) warning?
Indication of whether the request resulted in a warning from Sphinx.
Constructor Details
- (Search) initialize(*args)
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. @args = args add_default_scope unless [:ignore_default] populate if @options[:populate] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method, *args, &block)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/thinking_sphinx/search.rb', line 177 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, 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
- (Object) args (readonly)
Returns the value of attribute args
28 29 30 |
# File 'lib/thinking_sphinx/search.rb', line 28 def args @args end |
- (Object) options (readonly)
Returns the value of attribute options
28 29 30 |
# File 'lib/thinking_sphinx/search.rb', line 28 def @options end |
Class Method Details
+ (Object) bundle_searches(enum = nil)
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 |
+ (Object) count(*args)
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 |
+ (Object) facets(*args)
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 |
+ (Object) matching_fields(fields, bitmask)
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 |
+ (Object) search(*args)
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 |
+ (Object) search_for_id(*args)
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 |
+ (Object) search_for_ids(*args)
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 |
+ (Object) warn(message)
60 61 62 |
# File 'lib/thinking_sphinx/search.rb', line 60 def self.warn() ::ActiveSupport::Deprecation.warn end |
Instance Method Details
- (Object) ==(object)
98 99 100 101 |
# File 'lib/thinking_sphinx/search.rb', line 98 def ==(object) populate super end |
- (Object) all
Populates the search result set
109 110 111 112 |
# File 'lib/thinking_sphinx/search.rb', line 109 def all populate self end |
- (Object) append_to(client)
400 401 402 403 404 |
# File 'lib/thinking_sphinx/search.rb', line 400 def append_to(client) prepare client client.append_query query, indexes, comment client.reset end |
- (Object) as_json(*args)
120 121 122 123 |
# File 'lib/thinking_sphinx/search.rb', line 120 def as_json(*args) populate @array.as_json(*args) end |
- (Object) client
394 395 396 397 398 |
# File 'lib/thinking_sphinx/search.rb', line 394 def client client = [:client] || config.client prepare client end |
- (Integer) current_page
The current page number of the result set. Defaults to 1 if no page was explicitly requested.
212 213 214 |
# File 'lib/thinking_sphinx/search.rb', line 212 def current_page @options[:page].blank? ? 1 : @options[:page].to_i end |
- (Object) each_with_groupby_and_count(&block) Also known as: each_with_group_and_count
330 331 332 333 334 335 336 337 |
# File 'lib/thinking_sphinx/search.rb', line 330 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 |
- (Object) each_with_match(&block)
347 348 349 350 351 352 |
# File 'lib/thinking_sphinx/search.rb', line 347 def each_with_match(&block) populate results[:matches].each_with_index do |match, index| yield self[index], match end end |
- (Object) each_with_weighting(&block)
340 341 342 343 344 345 |
# File 'lib/thinking_sphinx/search.rb', line 340 def each_with_weighting(&block) populate results[:matches].each_with_index do |match, index| yield self[index], match[:weight] end end |
- (String?) error
The Sphinx-reported error, if any.
146 147 148 149 |
# File 'lib/thinking_sphinx/search.rb', line 146 def error populate @results[:error] end |
- (Boolean) error?
Indication of whether the request resulted in an error from Sphinx.
138 139 140 |
# File 'lib/thinking_sphinx/search.rb', line 138 def error? !!error end |
- (Object) excerpt_for(string, model = nil)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/thinking_sphinx/search.rb', line 354 def excerpt_for(string, model = nil) if model.nil? && one_class model ||= one_class end populate index = [:index] || "#{model.core_index_names.first}" client.excerpts( { :docs => [string.to_s], :words => results[:words].keys.join(' '), :index => index.split(',').first.strip }.merge([:excerpt_options] || {}) ).first end |
- (Object) facets(*args)
386 387 388 389 390 391 392 |
# File 'lib/thinking_sphinx/search.rb', line 386 def facets(*args) = args. merge_search self, args, args << ThinkingSphinx::FacetSearch.new(*args) end |
- (Boolean) first_page?
216 217 218 |
# File 'lib/thinking_sphinx/search.rb', line 216 def first_page? current_page == 1 end |
- (Object) freeze
114 115 116 117 118 |
# File 'lib/thinking_sphinx/search.rb', line 114 def freeze populate @array.freeze self end |
- (Object) indexes
321 322 323 324 325 326 327 328 |
# File 'lib/thinking_sphinx/search.rb', line 321 def indexes return [:index] if [:index] return '*' if classes.empty? classes.collect { |klass| klass.sphinx_index_names }.flatten.uniq.join(',') end |
- (Boolean) last_page?
239 240 241 |
# File 'lib/thinking_sphinx/search.rb', line 239 def last_page? next_page.nil? end |
- (Integer?) next_page
The next page number of the result set. If there are no more pages available, nil is returned.
231 232 233 |
# File 'lib/thinking_sphinx/search.rb', line 231 def next_page current_page >= total_pages ? nil : current_page + 1 end |
- (Boolean) next_page?
235 236 237 |
# File 'lib/thinking_sphinx/search.rb', line 235 def next_page? !next_page.nil? end |
- (Integer) offset Also known as: offset_value
The current page's offset, based on the number of records per page. Or explicit :offset if given.
315 316 317 |
# File 'lib/thinking_sphinx/search.rb', line 315 def offset @options[:offset] || ((current_page - 1) * per_page) end |
- (Object) page(page_number)
Kaminari support
221 222 223 224 |
# File 'lib/thinking_sphinx/search.rb', line 221 def page(page_number) @options[:page] = page_number self end |
- (Object) per(limit)
Kaminari support
266 267 268 269 |
# File 'lib/thinking_sphinx/search.rb', line 266 def per(limit) @options[:limit] = limit self end |
- (Integer) per_page Also known as: limit_value
The amount of records per set of paged results. Defaults to 20 unless a specific page size is requested.
257 258 259 260 261 |
# File 'lib/thinking_sphinx/search.rb', line 257 def per_page @options[:limit] ||= @options[:per_page] @options[:limit] ||= 20 @options[:limit].to_i end |
- (Object) populate_from_queue(results)
406 407 408 409 410 411 412 |
# File 'lib/thinking_sphinx/search.rb', line 406 def populate_from_queue(results) return if @populated @populated = true @results = results compose_results end |
- (Boolean) populated?
Indication of whether the request has been made to Sphinx for the search query.
130 131 132 |
# File 'lib/thinking_sphinx/search.rb', line 130 def populated? !!@populated end |
- (Integer?) previous_page
The previous page number of the result set. If this is the first page, then nil is returned.
248 249 250 |
# File 'lib/thinking_sphinx/search.rb', line 248 def previous_page current_page == 1 ? nil : current_page - 1 end |
- (Integer) query_time
Query time taken
289 290 291 292 293 294 |
# File 'lib/thinking_sphinx/search.rb', line 289 def query_time populate return 0 if @results[:time].nil? @query_time ||= @results[:time] end |
- (Boolean) respond_to?(method, include_private = false)
Returns true if the Search object or the underlying Array object respond to the requested method.
203 204 205 |
# File 'lib/thinking_sphinx/search.rb', line 203 def respond_to?(method, include_private = false) super || @array.respond_to?(method, include_private) end |
- (Hash) results
The query result hash from Riddle.
172 173 174 175 |
# File 'lib/thinking_sphinx/search.rb', line 172 def results populate @results end |
- (Object) search(*args)
371 372 373 374 375 |
# File 'lib/thinking_sphinx/search.rb', line 371 def search(*args) args << args..merge(:ignore_default => true) merge_search ThinkingSphinx::Search.new(*args), self.args, self end |
- (Object) search_for_ids(*args)
377 378 379 380 381 382 383 384 |
# File 'lib/thinking_sphinx/search.rb', line 377 def search_for_ids(*args) args << args..merge( :ignore_default => true, :ids_only => true ) merge_search ThinkingSphinx::Search.new(*args), self.args, self end |
- (Object) to_a
103 104 105 106 |
# File 'lib/thinking_sphinx/search.rb', line 103 def to_a populate @array end |
- (Integer) total_entries Also known as: total_count
The total number of search results available.
300 301 302 303 304 305 |
# File 'lib/thinking_sphinx/search.rb', line 300 def total_entries populate return 0 if @results.nil? || @results[:total_found].nil? @total_entries ||= @results[:total_found] end |
- (Integer) total_pages Also known as: page_count, num_pages
The total number of pages available if the results are paginated.
275 276 277 278 279 280 |
# File 'lib/thinking_sphinx/search.rb', line 275 def total_pages populate return 0 if @results.nil? || @results[:total].nil? @total_pages ||= (@results[:total] / per_page.to_f).ceil end |
- (String?) warning
The Sphinx-reported warning, if any.
163 164 165 166 |
# File 'lib/thinking_sphinx/search.rb', line 163 def warning populate @results[:warning] end |
- (Boolean) warning?
Indication of whether the request resulted in a warning from Sphinx.
155 156 157 |
# File 'lib/thinking_sphinx/search.rb', line 155 def warning? !!warning end |