Class: Sunspot::Search
- Inherits:
-
Object
- Object
- Sunspot::Search
- Defined in:
- lib/sunspot/search.rb,
lib/sunspot/search/hit.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.
Defined Under Namespace
Classes: Hit
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Query information for this search.
Instance Method Summary collapse
-
#build(&block) ⇒ Object
Build this search using a DSL block.
-
#data_accessor_for(clazz) ⇒ Object
Get the data accessor that will be used to load a particular class out of persistent storage.
-
#dynamic_facet(base_name, dynamic_name) ⇒ Object
Get the facet object for a given dynamic field.
-
#execute! ⇒ Object
Execute the search on the Solr instance and store the results.
-
#facet(field_name) ⇒ Object
Get the facet object for the given field.
-
#hits ⇒ Object
(also: #raw_results)
Access raw Solr result information.
-
#initialize(connection, setup, query) ⇒ Search
constructor
:nodoc:.
-
#populate_hits! ⇒ Object
:nodoc:.
-
#results ⇒ Object
Get the collection of results as instantiated objects.
-
#total ⇒ Object
The total number of documents matching the query parameters.
Constructor Details
#initialize(connection, setup, query) ⇒ Search
:nodoc:
17 18 19 |
# File 'lib/sunspot/search.rb', line 17 def initialize(connection, setup, query) #:nodoc: @connection, @setup, @query = connection, setup, query end |
Instance Attribute Details
#query ⇒ Object (readonly)
Query information for this search. If you wish to build the query without using the search DSL, this method allows you to access the query API directly. See Sunspot#new_search for how to construct the search object in this case.
15 16 17 |
# File 'lib/sunspot/search.rb', line 15 def query @query end |
Instance Method Details
#build(&block) ⇒ Object
Build this search using a DSL block.
155 156 157 158 |
# File 'lib/sunspot/search.rb', line 155 def build(&block) #:nodoc: Util.instance_eval_or_call(dsl, &block) self end |
#data_accessor_for(clazz) ⇒ Object
Get the data accessor that will be used to load a particular class out of persistent storage. Data accessors can implement any methods that may be useful for refining how data is loaded out of storage. When building a search manually (e.g., using the Sunspot#new_search method), this should be used before calling #execute(). Use the Sunspot::DSL::Search#data_accessor_for method when building searches using the block DSL.
147 148 149 150 |
# File 'lib/sunspot/search.rb', line 147 def data_accessor_for(clazz) (@data_accessors ||= {})[clazz.name.to_sym] ||= Adapters::DataAccessor.create(clazz) end |
#dynamic_facet(base_name, dynamic_name) ⇒ Object
Get the facet object for a given dynamic field. This dynamic field will need to have been requested as a field facet inside the search block.
Parameters
- base_name<Symbol>
-
Base name of the dynamic field definiton (as specified in the setup block)
- dynamic_name<Symbol>
-
Dynamic field name to facet on
Returns
- Sunspot::Facet
-
Facet object for given dynamic field
Example
search = Sunspot.search(Post) do
dynamic :custom do
facet :cuisine
end
end
search.dynamic_facet(:custom, :cuisine)
#=> Facet for the dynamic field :cuisine in the :custom field definition
130 131 132 133 134 135 136 |
# File 'lib/sunspot/search.rb', line 130 def dynamic_facet(base_name, dynamic_name) (@dynamic_facets_cache ||= {})[[base_name.to_sym, dynamic_name.to_sym]] ||= begin field = @setup.dynamic_field_factory(base_name).build(dynamic_name) Facet.new(FacetData::FieldFacetData.new(@solr_result['facet_counts']['facet_fields'][field.indexed_name], field)) end 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.
28 29 30 31 32 |
# File 'lib/sunspot/search.rb', line 28 def execute! params = @query.to_params @solr_result = @connection.select(params) self end |
#facet(field_name) ⇒ Object
Get the facet object for the given field. This field will need to have been requested as a field facet inside the search block.
Parameters
- field_name<Symbol>
-
field name for which to get the facet
Returns
- Sunspot::Facet
-
Facet object for the given field
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sunspot/search.rb', line 90 def facet(field_name) (@facets_cache ||= {})[field_name.to_sym] ||= begin facet_data = query_facet_data(field_name) || begin field = field(field_name) date_facet_data(field) || FacetData::FieldFacetData.new(@solr_result['facet_counts']['facet_fields'][field.indexed_name], field) end facet_class = facet_data.reference ? InstantiatedFacet : Facet facet_class.new(facet_data) end end |
#hits ⇒ 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.
Returns
- Array
-
Ordered collection of Hit objects
62 63 64 |
# File 'lib/sunspot/search.rb', line 62 def hits @hits ||= solr_response['docs'].map { |doc| Hit.new(doc, self) } end |
#populate_hits! ⇒ Object
:nodoc:
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/sunspot/search.rb', line 160 def populate_hits! #:nodoc: id_hit_hash = Hash.new { |h, k| h[k] = {} } hits.each do |hit| id_hit_hash[hit.class_name][hit.primary_key] = hit end id_hit_hash.each_pair do |class_name, hits| ids = hits.map { |id, hit| hit.primary_key } data_accessor_for(Util.full_const_get(class_name)).load_all(ids).each do |instance| hit = id_hit_hash[class_name][Adapters::InstanceAdapter.adapt(instance).id.to_s] hit.instance = instance end end 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.
Returns
- WillPaginate::Collection or Array
-
Instantiated result objects
43 44 45 46 47 48 49 50 51 |
# File 'lib/sunspot/search.rb', line 43 def results @results ||= if @query.page && defined?(WillPaginate::Collection) WillPaginate::Collection.create(@query.page, @query.per_page, total) do |pager| pager.replace(hits.map { |hit| hit.instance }) end else hits.map { |hit| hit.instance } end end |
#total ⇒ Object
The total number of documents matching the query parameters
Returns
- Integer
-
Total matching documents
74 75 76 |
# File 'lib/sunspot/search.rb', line 74 def total @total ||= solr_response['numFound'] end |