Class: Sunspot::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/search.rb,
lib/sunspot/search/hit.rb,
lib/sunspot/search/highlight.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: Highlight, Hit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, setup, query) ⇒ Search

:nodoc:



19
20
21
# File 'lib/sunspot/search.rb', line 19

def initialize(connection, setup, query) #:nodoc:
  @connection, @setup, @query = connection, setup, query
end

Instance Attribute Details

#queryObject (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.



17
18
19
# File 'lib/sunspot/search.rb', line 17

def query
  @query
end

Instance Method Details

#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!


161
162
163
164
# File 'lib/sunspot/search.rb', line 161

def build(&block)
  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.



143
144
145
146
# File 'lib/sunspot/search.rb', line 143

def data_accessor_for(clazz) #:nodoc:
  (@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


126
127
128
129
130
131
132
# File 'lib/sunspot/search.rb', line 126

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

#executeObject Also known as: execute!

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.



30
31
32
33
34
# File 'lib/sunspot/search.rb', line 30

def execute
  params = @query.to_params
  @solr_result = @connection.select(params)
  self
end

#facet(name) ⇒ 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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sunspot/search.rb', line 86

def facet(name)
  (@facets_cache ||= {})[name.to_sym] ||=
    begin
      facet_data = query_facet_data(name) ||
        begin
          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

#hitsObject 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



65
66
67
# File 'lib/sunspot/search.rb', line 65

def hits
  @hits ||= solr_response['docs'].map { |doc| Hit.new(doc, highlights_for(doc), self) }
end

#inspectObject

:nodoc:



185
186
187
# File 'lib/sunspot/search.rb', line 185

def inspect #:nodoc:
  "<Sunspot::Search:#{query.to_params.inspect}>"
end

#populate_hits!Object

Populate the Hit objects with their instances. This is invoked the first time any hit has its instance requested, and all hits are loaded as a batch.



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sunspot/search.rb', line 171

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

#resultsObject

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



46
47
48
49
50
51
52
53
54
# File 'lib/sunspot/search.rb', line 46

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

#totalObject

The total number of documents matching the query parameters

Returns

Integer

Total matching documents



77
78
79
# File 'lib/sunspot/search.rb', line 77

def total
  @total ||= solr_response['numFound']
end