Class: ThinkingSphinx::Search
- Inherits:
-
Object
- Object
- 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 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .bundle_searches(enum = nil) ⇒ Object
-
.count(*args) ⇒ Object
Deprecated.
-
.facets(*args) ⇒ Object
Deprecated.
- .matching_fields(fields, bitmask) ⇒ Object
-
.search(*args) ⇒ Object
Deprecated.
-
.search_for_id(*args) ⇒ Object
Deprecated.
-
.search_for_ids(*args) ⇒ Object
Deprecated.
Instance Method Summary collapse
- #append_to(client) ⇒ Object
- #client ⇒ Object
-
#current_page ⇒ Integer
The current page number of the result set.
- #each_with_groupby_and_count(&block) ⇒ Object (also: #each_with_group_and_count)
- #each_with_match(&block) ⇒ Object
- #each_with_weighting(&block) ⇒ Object
- #excerpt_for(string, model = nil) ⇒ Object
- #freeze ⇒ Object
- #indexes ⇒ Object
-
#initialize(*args) ⇒ Search
constructor
A new instance of Search.
- #method_missing(method, *args, &block) ⇒ Object
-
#next_page ⇒ Integer?
The next page number of the result set.
-
#offset ⇒ Integer
The current page’s offset, based on the number of records per page.
-
#per_page ⇒ Integer
The amount of records per set of paged results.
- #populate_from_queue(results) ⇒ Object
-
#populated? ⇒ Boolean
Indication of whether the request has been made to Sphinx for the search query.
-
#previous_page ⇒ Integer?
The previous page number of the result set.
-
#query_time ⇒ Integer
Query time taken.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Returns true if the Search object or the underlying Array object respond to the requested method.
-
#results ⇒ Hash
The query result hash from Riddle.
- #search(*args) ⇒ Object
- #to_a ⇒ Object
-
#total_entries ⇒ Integer
The total number of search results available.
-
#total_pages ⇒ Integer
(also: #page_count)
The total number of pages available if the results are paginated.
Constructor Details
#initialize(*args) ⇒ Search
Returns a new instance of Search.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/thinking_sphinx/search.rb', line 82 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
#method_missing(method, *args, &block) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/thinking_sphinx/search.rb', line 123 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) 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
#args ⇒ Object (readonly)
Returns the value of attribute args.
28 29 30 |
# File 'lib/thinking_sphinx/search.rb', line 28 def args @args end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/thinking_sphinx/search.rb', line 28 def @options end |
Class Method Details
.bundle_searches(enum = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/thinking_sphinx/search.rb', line 60 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) 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
72 73 74 75 76 77 78 79 80 |
# File 'lib/thinking_sphinx/search.rb', line 72 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
#append_to(client) ⇒ Object
293 294 295 296 297 |
# File 'lib/thinking_sphinx/search.rb', line 293 def append_to(client) prepare client client.append_query query, indexes, comment client.reset end |
#client ⇒ Object
287 288 289 290 291 |
# File 'lib/thinking_sphinx/search.rb', line 287 def client client = [:client] || config.client prepare client end |
#current_page ⇒ Integer
The current page number of the result set. Defaults to 1 if no page was explicitly requested.
158 159 160 |
# File 'lib/thinking_sphinx/search.rb', line 158 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
244 245 246 247 248 249 250 251 |
# File 'lib/thinking_sphinx/search.rb', line 244 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
261 262 263 264 265 266 |
# File 'lib/thinking_sphinx/search.rb', line 261 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
254 255 256 257 258 259 |
# File 'lib/thinking_sphinx/search.rb', line 254 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
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/thinking_sphinx/search.rb', line 268 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 => [:index] || "#{model.source_of_sphinx_index.sphinx_name}_core" ).first end |
#freeze ⇒ Object
99 100 101 102 103 |
# File 'lib/thinking_sphinx/search.rb', line 99 def freeze populate @array.freeze self end |
#indexes ⇒ Object
235 236 237 238 239 240 241 242 |
# File 'lib/thinking_sphinx/search.rb', line 235 def indexes return [:index] if [:index] return '*' if classes.empty? classes.collect { |klass| klass.sphinx_index_names }.flatten.uniq.join(',') end |
#next_page ⇒ Integer?
The next page number of the result set. If there are no more pages available, nil is returned.
167 168 169 |
# File 'lib/thinking_sphinx/search.rb', line 167 def next_page current_page >= total_pages ? nil : current_page + 1 end |
#offset ⇒ Integer
The current page’s offset, based on the number of records per page. Or explicit :offset if given.
231 232 233 |
# File 'lib/thinking_sphinx/search.rb', line 231 def offset @options[:offset] || ((current_page - 1) * per_page) end |
#per_page ⇒ Integer
The amount of records per set of paged results. Defaults to 20 unless a specific page size is requested.
185 186 187 188 189 |
# File 'lib/thinking_sphinx/search.rb', line 185 def per_page @options[:limit] ||= @options[:per_page] @options[:limit] ||= 20 @options[:limit].to_i end |
#populate_from_queue(results) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/thinking_sphinx/search.rb', line 299 def populate_from_queue(results) return if @populated @populated = true @results = results if [:ids_only] replace @results[:matches].collect { |match| match[:attributes]["sphinx_internal_id"] } else replace instances_from_matches add_excerpter add_sphinx_attributes add_matching_fields if client.rank_mode == :fieldmask end end |
#populated? ⇒ Boolean
Indication of whether the request has been made to Sphinx for the search query.
110 111 112 |
# File 'lib/thinking_sphinx/search.rb', line 110 def populated? !!@populated end |
#previous_page ⇒ Integer?
The previous page number of the result set. If this is the first page, then nil is returned.
176 177 178 |
# File 'lib/thinking_sphinx/search.rb', line 176 def previous_page current_page == 1 ? nil : current_page - 1 end |
#query_time ⇒ Integer
Query time taken
208 209 210 211 212 213 |
# File 'lib/thinking_sphinx/search.rb', line 208 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.
149 150 151 |
# File 'lib/thinking_sphinx/search.rb', line 149 def respond_to?(method, include_private = false) super || @array.respond_to?(method, include_private) end |
#results ⇒ Hash
The query result hash from Riddle.
118 119 120 121 |
# File 'lib/thinking_sphinx/search.rb', line 118 def results populate @results end |
#search(*args) ⇒ Object
281 282 283 284 285 |
# File 'lib/thinking_sphinx/search.rb', line 281 def search(*args) args << args..merge(:ignore_default => true) merge_search ThinkingSphinx::Search.new(*args) self end |
#to_a ⇒ Object
94 95 96 97 |
# File 'lib/thinking_sphinx/search.rb', line 94 def to_a populate @array end |
#total_entries ⇒ Integer
The total number of search results available.
219 220 221 222 223 224 |
# File 'lib/thinking_sphinx/search.rb', line 219 def total_entries populate return 0 if @results[:total_found].nil? @total_entries ||= @results[:total_found] end |
#total_pages ⇒ Integer Also known as: page_count
The total number of pages available if the results are paginated.
195 196 197 198 199 200 |
# File 'lib/thinking_sphinx/search.rb', line 195 def total_pages populate return 0 if @results[:total].nil? @total_pages ||= (@results[:total] / per_page.to_f).ceil end |