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 Method Summary collapse
-
#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, configuration, *types, &block) ⇒ 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, configuration, *types, &block) ⇒ Search
:nodoc:
10 11 12 13 14 15 16 |
# File 'lib/sunspot/search.rb', line 10 def initialize(connection, configuration, *types, &block) #:nodoc: @connection = connection params = types.last.is_a?(Hash) ? types.pop : {} @query = Query.new(types, params, configuration) @query.dsl.instance_eval(&block) if block @types = types end |
Instance Method Details
#execute! ⇒ Object
Execute the search on the Solr instance and store the results
21 22 23 24 25 |
# File 'lib/sunspot/search.rb', line 21 def execute! #:nodoc: 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
83 84 85 86 87 88 89 |
# File 'lib/sunspot/search.rb', line 83 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
56 57 58 |
# File 'lib/sunspot/search.rb', line 56 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
36 37 38 39 40 41 42 43 44 |
# File 'lib/sunspot/search.rb', line 36 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
67 68 69 |
# File 'lib/sunspot/search.rb', line 67 def total @total ||= @solr_result.total_hits end |