class Sunspot::Search::AbstractSearch

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.

Attributes

facets[R]

Retrieve all facet objects defined for this search, in order they were defined. To retrieve an individual facet by name, use facet()

groups[R]

Retrieve all facet objects defined for this search, in order they were defined. To retrieve an individual facet by name, use facet()

request_handler[RW]

Public Instance Methods

add_json_facet(field, options = {}) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 266
def add_json_facet(field, options = {})
  name = (options[:name] || field.name)
  add_facet(name, FieldJsonFacet.new(field, self, options))
end
build(&block) click to toggle source

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
# File lib/sunspot/search/abstract_search.rb, line 225
def build(&block)
  Util.instance_eval_or_call(dsl, &block)
  self
end
execute() click to toggle source

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.

# 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
facet(name, dynamic_name = nil) click to toggle source

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
# 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
group(name) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 179
def group(name)
  if name
    @groups_by_name[name.to_sym]
  end
end
hits(options = {}) click to toggle source

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

Calls superclass method
# File lib/sunspot/search/abstract_search.rb, line 90
def hits(options = {})
  if options[:verify]
    super
  else
    @hits ||= paginate_collection(super)
  end
end
Also aliased as: raw_results
json_facet_stats(name, options = {}) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 185
def json_facet_stats(name, options = {})
  JsonFacetStats.new(name, self, options)
end
query_time() click to toggle source

The time elapsed to generate the Solr response

Returns

Integer

Query runtime in milliseconds

# File lib/sunspot/search/abstract_search.rb, line 117
def query_time
  @query_time ||= solr_response_header['QTime']
end
raw_results(options = {})
Alias for: hits
results() click to toggle source

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

# File lib/sunspot/search/abstract_search.rb, line 69
def results
  @results ||= paginate_collection(verified_hits.map { |hit| hit.instance })
end
stats(name) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 173
def stats(name)
  if name
    @stats_by_name[name.to_sym]
  end
end
total() click to toggle source

The total number of documents matching the query parameters

Returns

Integer

Total matching documents

# File lib/sunspot/search/abstract_search.rb, line 106
def total
  @total ||= solr_response['numFound'] || 0
end

Private Instance Methods

add_facet(name, facet) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 315
def add_facet(name, facet)
  @facets << facet
  @facets_by_name[name.to_sym] = facet
end
add_stats(name, stats) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 320
def add_stats(name, stats)
  @stats << stats
  @stats_by_name[name.to_sym] = stats
end
add_subgroup(name, group) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 325
def add_subgroup(name, group)
  @groups << group
  @groups_by_name[name.to_sym] = group
end
dsl() click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 279
def dsl
  raise NotImplementedError
end
execute_request(params) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 283
def execute_request(params)
  raise NotImplementedError
end
next_cursor() click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 299
def next_cursor
  @solr_result['nextCursorMark'] if @query.cursor
end
paginate_collection(collection) click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 307
def paginate_collection(collection)
  if @query.cursor
    CursorPaginatedCollection.new(collection, @query.per_page, total, @query.cursor, next_cursor)
  else
    PaginatedCollection.new(collection, @query.page, @query.per_page, total)
  end
end
reset() click to toggle source

Clear out all the cached ivars so the search can be called again.

# File lib/sunspot/search/abstract_search.rb, line 331
def reset
  @results = @hits = @verified_hits = @total = @solr_response = @doc_ids = nil
end
solr_docs() click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 295
def solr_docs
  solr_response['docs']
end
solr_response() click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 287
def solr_response
  @solr_response ||= @solr_result['response'] || {}
end
solr_response_header() click to toggle source
# File lib/sunspot/search/abstract_search.rb, line 291
def solr_response_header
  @solr_response_header ||= @solr_result['responseHeader'] || {}
end
verified_hits() click to toggle source
Calls superclass method
# File lib/sunspot/search/abstract_search.rb, line 303
def verified_hits
  @verified_hits ||= paginate_collection(super)
end