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 )
- 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.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Class Method Summary collapse
-
.count(*args) ⇒ Object
Deprecated.
-
.facets(*args) ⇒ Object
Deprecated.
-
.search(*args) ⇒ Object
Deprecated.
-
.search_for_id(*args) ⇒ Object
Deprecated.
-
.search_for_ids(*args) ⇒ Object
Deprecated.
Instance Method Summary collapse
-
#current_page ⇒ Integer
The current page number of the result set.
- #each_with_groupby_and_count(&block) ⇒ Object
- #each_with_weighting(&block) ⇒ Object
- #excerpt_for(string, model = nil) ⇒ 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.
-
#previous_page ⇒ Integer?
The previous page number of the result set.
-
#respond_to?(method) ⇒ Boolean
Returns true if the Search object or the underlying Array object respond to the requested method.
- #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.
60 61 62 63 64 |
# File 'lib/thinking_sphinx/search.rb', line 60 def initialize(*args) @array = [] @options = args. @args = args end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/thinking_sphinx/search.rb', line 71 def method_missing(method, *args, &block) if is_scope?(method) add_scope(method, *args, &block) return self 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 |
#results ⇒ Object (readonly)
Returns the value of attribute results.
28 29 30 |
# File 'lib/thinking_sphinx/search.rb', line 28 def results @results 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 |
.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_page ⇒ Integer
The current page number of the result set. Defaults to 1 if no page was explicitly requested.
103 104 105 |
# File 'lib/thinking_sphinx/search.rb', line 103 def current_page @options[:page].blank? ? 1 : @options[:page].to_i end |
#each_with_groupby_and_count(&block) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/thinking_sphinx/search.rb', line 161 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
170 171 172 173 174 175 |
# File 'lib/thinking_sphinx/search.rb', line 170 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
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/thinking_sphinx/search.rb', line 177 def excerpt_for(string, model = nil) if model.nil? && [:classes].length == 1 model ||= [:classes].first end populate client.excerpts( :docs => [string], :words => results[:words].keys.join(' '), :index => "#{model.sphinx_name}_core" ).first end |
#next_page ⇒ Integer?
The next page number of the result set. If there are no more pages available, nil is returned.
112 113 114 |
# File 'lib/thinking_sphinx/search.rb', line 112 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.
157 158 159 |
# File 'lib/thinking_sphinx/search.rb', line 157 def 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.
130 131 132 |
# File 'lib/thinking_sphinx/search.rb', line 130 def per_page @options[:limit] || @options[:per_page] || 20 end |
#previous_page ⇒ Integer?
The previous page number of the result set. If this is the first page, then nil is returned.
121 122 123 |
# File 'lib/thinking_sphinx/search.rb', line 121 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.
94 95 96 |
# File 'lib/thinking_sphinx/search.rb', line 94 def respond_to?(method) super || @array.respond_to?(method) end |
#to_a ⇒ Object
66 67 68 69 |
# File 'lib/thinking_sphinx/search.rb', line 66 def to_a populate @array end |
#total_entries ⇒ Integer
The total number of search results available.
148 149 150 151 |
# File 'lib/thinking_sphinx/search.rb', line 148 def total_entries populate @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.
138 139 140 |
# File 'lib/thinking_sphinx/search.rb', line 138 def total_pages @total_pages ||= (total_entries / per_page.to_f).ceil end |