Class: Sunspot::Search::AbstractSearch
- Inherits:
-
Object
- Object
- Sunspot::Search::AbstractSearch
- Includes:
- HitEnumerable
- Defined in:
- lib/sunspot/search/abstract_search.rb
Overview
This class encapsulates the results of a Solr search. It provides access to search results, total result count, facets, and pagination information. Instances of Search are returned by the Sunspot.search and Sunspot.new_search methods.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#facets ⇒ Object
readonly
Retrieve all facet objects defined for this search, in order they were defined.
-
#groups ⇒ Object
readonly
Retrieve all facet objects defined for this search, in order they were defined.
-
#query ⇒ Object
readonly
:nodoc:.
-
#request_handler ⇒ Object
Returns the value of attribute request_handler.
Instance Method Summary collapse
-
#add_date_facet(field, options) ⇒ Object
:nodoc:.
-
#add_field_facet(field, options = {}) ⇒ Object
:nodoc:.
-
#add_field_stats(field) ⇒ Object
:nodoc:.
-
#add_group(group) ⇒ Object
:nodoc:.
- #add_json_facet(field, options = {}) ⇒ Object
-
#add_query_facet(name, options) ⇒ Object
:nodoc:.
-
#add_range_facet(field, options) ⇒ Object
:nodoc:.
-
#build(&block) ⇒ Object
Build this search using a DSL block.
-
#dynamic_facet(base_name, dynamic_name) ⇒ Object
Deprecated in favor of optional second argument to #facet.
-
#execute ⇒ Object
Execute the search on the Solr instance and store the results.
-
#execute! ⇒ Object
:nodoc: deprecated.
-
#facet(name, dynamic_name = nil) ⇒ Object
Get the facet object for the given name.
-
#facet_response ⇒ Object
:nodoc:.
- #group(name) ⇒ Object
-
#group_response ⇒ Object
:nodoc:.
-
#highlights_for(doc) ⇒ Object
:nodoc:.
-
#hits(options = {}) ⇒ Object
(also: #raw_results)
Access raw Solr result information.
-
#initialize(connection, setup, query, configuration) ⇒ AbstractSearch
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#json_facet_response ⇒ Object
:nodoc:.
- #json_facet_stats(name, options = {}) ⇒ Object
-
#query_time ⇒ Object
The time elapsed to generate the Solr response.
-
#results ⇒ Object
Get the collection of results as instantiated objects.
- #stats(name) ⇒ Object
-
#stats_response ⇒ Object
:nodoc:.
-
#total ⇒ Object
The total number of documents matching the query parameters.
Methods included from HitEnumerable
#data_accessor_for, #each_hit_with_result, #populate_hits
Constructor Details
#initialize(connection, setup, query, configuration) ⇒ AbstractSearch
:nodoc:
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/sunspot/search/abstract_search.rb', line 25 def initialize(connection, setup, query, configuration) #:nodoc: @connection, @setup, @query = connection, setup, query @query.paginate(1, configuration.pagination.default_per_page) @facets = [] @facets_by_name = {} @groups_by_name = {} @groups = [] @stats_by_name = {} @stats = [] end |
Instance Attribute Details
#facets ⇒ Object (readonly)
Retrieve all facet objects defined for this search, in order they were defined. To retrieve an individual facet by name, use #facet()
19 20 21 |
# File 'lib/sunspot/search/abstract_search.rb', line 19 def facets @facets end |
#groups ⇒ Object (readonly)
Retrieve all facet objects defined for this search, in order they were defined. To retrieve an individual facet by name, use #facet()
19 20 21 |
# File 'lib/sunspot/search/abstract_search.rb', line 19 def groups @groups end |
#query ⇒ Object (readonly)
:nodoc:
20 21 22 |
# File 'lib/sunspot/search/abstract_search.rb', line 20 def query @query end |
#request_handler ⇒ Object
Returns the value of attribute request_handler.
21 22 23 |
# File 'lib/sunspot/search/abstract_search.rb', line 21 def request_handler @request_handler end |
Instance Method Details
#add_date_facet(field, options) ⇒ Object
:nodoc:
252 253 254 255 |
# File 'lib/sunspot/search/abstract_search.rb', line 252 def add_date_facet(field, ) #:nodoc: name = ([:name] || field.name) add_facet(name, DateFacet.new(field, self, )) end |
#add_field_facet(field, options = {}) ⇒ Object
:nodoc:
243 244 245 246 |
# File 'lib/sunspot/search/abstract_search.rb', line 243 def add_field_facet(field, = {}) #:nodoc: name = ([:name] || field.name) add_facet(name, FieldFacet.new(field, self, )) end |
#add_field_stats(field) ⇒ Object
:nodoc:
262 263 264 |
# File 'lib/sunspot/search/abstract_search.rb', line 262 def add_field_stats(field) #:nodoc: add_stats(field.name, FieldStats.new(field, self)) end |
#add_group(group) ⇒ Object
:nodoc:
235 236 237 238 239 240 241 |
# File 'lib/sunspot/search/abstract_search.rb', line 235 def add_group(group) #:nodoc: group.fields.each do |field| add_subgroup(field.name, FieldGroup.new(field, self)) end add_subgroup(:queries, QueryGroup.new(group.queries, self)) if group.queries.any? end |
#add_json_facet(field, options = {}) ⇒ Object
266 267 268 269 |
# File 'lib/sunspot/search/abstract_search.rb', line 266 def add_json_facet(field, = {}) name = ([:name] || field.name) add_facet(name, FieldJsonFacet.new(field, self, )) end |
#add_query_facet(name, options) ⇒ Object
:nodoc:
248 249 250 |
# File 'lib/sunspot/search/abstract_search.rb', line 248 def add_query_facet(name, ) #:nodoc: add_facet(name, QueryFacet.new(name, self, )) end |
#add_range_facet(field, options) ⇒ Object
:nodoc:
257 258 259 260 |
# File 'lib/sunspot/search/abstract_search.rb', line 257 def add_range_facet(field, ) #:nodoc: name = ([:name] || field.name) add_facet(name, RangeFacet.new(field, self, )) end |
#build(&block) ⇒ Object
Build this search using a DSL block. This method can be called more than once on an unexecuted search (e.g., Sunspot.new_search) in order to build a search incrementally.
Example
search = Sunspot.new_search(Post)
search.build do
with(:published_at).less_than Time.now
end
search.execute
225 226 227 228 |
# File 'lib/sunspot/search/abstract_search.rb', line 225 def build(&block) Util.instance_eval_or_call(dsl, &block) self end |
#dynamic_facet(base_name, dynamic_name) ⇒ Object
Deprecated in favor of optional second argument to #facet
192 193 194 |
# File 'lib/sunspot/search/abstract_search.rb', line 192 def dynamic_facet(base_name, dynamic_name) #:nodoc: facet(base_name, dynamic_name) end |
#execute ⇒ Object
Execute the search on the Solr instance and store the results. If you use Sunspot#search() to construct your searches, there is no need to call this method as it has already been called. If you use Sunspot#new_search(), you will need to call this method after building the query.
46 47 48 49 50 51 |
# File 'lib/sunspot/search/abstract_search.rb', line 46 def execute reset params = @query.to_params @solr_result = @connection.post "#{request_handler}", :data => params self end |
#execute! ⇒ Object
:nodoc: deprecated
53 54 55 |
# File 'lib/sunspot/search/abstract_search.rb', line 53 def execute! #:nodoc: deprecated execute end |
#facet(name, dynamic_name = nil) ⇒ Object
Get the facet object for the given name. ‘name` can either be the name given to a query facet, or the field name of a field facet. Returns a Sunspot::Facet object.
Parameters
- name<Symbol>
-
Name of the field to return the facet for, or the name given to the query facet when the search was constructed.
- dynamic_name<Symbol>
-
If faceting on a dynamic field, this is the dynamic portion of the field name.
Example:
search = Sunspot.search(Post) do
facet :category_ids
dynamic :custom do
facet :cuisine
end
facet :age do
row 'Less than a month' do
with(:published_at).greater_than(1.month.ago)
end
row 'Less than a year' do
with(:published_at, 1.year.ago..1.month.ago)
end
row 'More than a year' do
with(:published_at).less_than(1.year.ago)
end
end
end
search.facet(:category_ids)
#=> Facet for :category_ids field
search.facet(:custom, :cuisine)
#=> Facet for the dynamic field :cuisine in the :custom field definition
search.facet(:age)
#=> Facet for the query facet named :age
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/sunspot/search/abstract_search.rb', line 161 def facet(name, dynamic_name = nil) if name facet_name = if dynamic_name separator = @setup.dynamic_field_factory(name).separator [name, dynamic_name].join(separator) else name end.to_sym @facets_by_name[facet_name] end end |
#facet_response ⇒ Object
:nodoc:
196 197 198 |
# File 'lib/sunspot/search/abstract_search.rb', line 196 def facet_response #:nodoc: @solr_result['facet_counts'] end |
#group(name) ⇒ Object
179 180 181 182 183 |
# File 'lib/sunspot/search/abstract_search.rb', line 179 def group(name) if name @groups_by_name[name.to_sym] end end |
#group_response ⇒ Object
:nodoc:
208 209 210 |
# File 'lib/sunspot/search/abstract_search.rb', line 208 def group_response #:nodoc: @solr_result['grouped'] end |
#highlights_for(doc) ⇒ Object
:nodoc:
271 272 273 274 275 |
# File 'lib/sunspot/search/abstract_search.rb', line 271 def highlights_for(doc) #:nodoc: if @solr_result['highlighting'] @solr_result['highlighting'][doc['id']] end end |
#hits(options = {}) ⇒ Object Also known as: raw_results
Access raw Solr result information. Returns a collection of Hit objects that contain the class name, primary key, keyword relevance score (if applicable), and any stored fields.
Options (options)
- :verify
-
Only return hits that reference objects that actually exist in the data store. This causes results to be eager-loaded from the data store, unlike the normal behavior of this method, which only loads the referenced results when Hit#result is first called.
Returns
- Array
-
Ordered collection of Hit objects
90 91 92 93 94 95 96 |
# File 'lib/sunspot/search/abstract_search.rb', line 90 def hits( = {}) if [:verify] super else @hits ||= paginate_collection(super) end end |
#inspect ⇒ Object
:nodoc:
231 232 233 |
# File 'lib/sunspot/search/abstract_search.rb', line 231 def inspect #:nodoc: "<Sunspot::Search:#{query.to_params.inspect}>" end |
#json_facet_response ⇒ Object
:nodoc:
200 201 202 |
# File 'lib/sunspot/search/abstract_search.rb', line 200 def json_facet_response #:nodoc: @solr_result['facets'] end |
#json_facet_stats(name, options = {}) ⇒ Object
185 186 187 |
# File 'lib/sunspot/search/abstract_search.rb', line 185 def json_facet_stats(name, = {}) JsonFacetStats.new(name, self, ) end |
#query_time ⇒ Object
The time elapsed to generate the Solr response
Returns
- Integer
-
Query runtime in milliseconds
117 118 119 |
# File 'lib/sunspot/search/abstract_search.rb', line 117 def query_time @query_time ||= solr_response_header['QTime'] end |
#results ⇒ Object
Get the collection of results as instantiated objects. If WillPaginate is available, the results will be a WillPaginate::Collection instance; if not, it will be a vanilla Array.
If not all of the results referenced by the Solr hits actually exist in the data store, Sunspot will only return the results that do exist.
Returns
- WillPaginate::Collection or Array
-
Instantiated result objects
69 70 71 |
# File 'lib/sunspot/search/abstract_search.rb', line 69 def results @results ||= paginate_collection(verified_hits.map { |hit| hit.instance }) end |
#stats(name) ⇒ Object
173 174 175 176 177 |
# File 'lib/sunspot/search/abstract_search.rb', line 173 def stats(name) if name @stats_by_name[name.to_sym] end end |
#stats_response ⇒ Object
:nodoc:
204 205 206 |
# File 'lib/sunspot/search/abstract_search.rb', line 204 def stats_response #:nodoc: @solr_result['stats']['stats_fields'] end |
#total ⇒ Object
The total number of documents matching the query parameters
Returns
- Integer
-
Total matching documents
106 107 108 |
# File 'lib/sunspot/search/abstract_search.rb', line 106 def total @total ||= solr_response['numFound'] || 0 end |