Class: Sunspot::Search
- Inherits:
-
Object
- Object
- Sunspot::Search
- Defined in:
- lib/sunspot/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 method.
Defined Under Namespace
Classes: RawResult
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Query information for this search.
Instance Method Summary collapse
-
#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.
-
#initialize(connection, query) ⇒ Search
constructor
:nodoc:.
-
#raw_results ⇒ Object
Access raw results without instantiating objects from persistent storage.
-
#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, query) ⇒ Search
:nodoc:
17 18 19 20 |
# File 'lib/sunspot/search.rb', line 17 def initialize(connection, query) #:nodoc: @connection = connection @query = 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
#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
125 126 127 128 129 130 131 |
# File 'lib/sunspot/search.rb', line 125 def dynamic_facet(base_name, dynamic_name) (@dynamic_facets_cache ||= {})[[base_name.to_sym, dynamic_name.to_sym]] ||= begin field = @query.dynamic_field(base_name).build(dynamic_name) Facet.new(@solr_result.field_facets(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.
29 30 31 32 33 |
# File 'lib/sunspot/search.rb', line 29 def execute! params = @query.to_params @solr_result = @connection.query(params.delete(:q), 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
91 92 93 94 95 96 97 |
# File 'lib/sunspot/search.rb', line 91 def facet(field_name) (@facets_cache ||= {})[field_name.to_sym] ||= begin field = @query.field(field_name) Facet.new(@solr_result.field_facets(field.indexed_name), field) end end |
#raw_results ⇒ Object
Access raw results without instantiating objects from persistent storage. This may be useful if you are using search as an intermediate step in data retrieval. Returns an ordered collection of objects that respond to #class_name and #primary_key
Returns
- Array
-
Ordered collection of raw results
64 65 66 |
# File 'lib/sunspot/search.rb', line 64 def raw_results @raw_results ||= hit_ids.map { |hit_id| RawResult.new(*hit_id.match(/([^ ]+) (.+)/)[1..2]) } 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
44 45 46 47 48 49 50 51 52 |
# File 'lib/sunspot/search.rb', line 44 def results @results ||= if @query.page && defined?(WillPaginate::Collection) WillPaginate::Collection.create(@query.page, @query.per_page, @solr_result.total_hits) do |pager| pager.replace(result_objects) end else result_objects end end |
#total ⇒ Object
The total number of documents matching the query parameters
Returns
- Integer
-
Total matching documents
75 76 77 |
# File 'lib/sunspot/search.rb', line 75 def total @total ||= @solr_result.total_hits end |