Class: Sunspot::DSL::Query

Inherits:
FieldQuery show all
Defined in:
lib/sunspot/dsl/query.rb

Overview

This class presents a DSL for constructing queries using the Sunspot.search method. Methods of this class are available inside the search block. Much of the DSL’s functionality is implemented by this class’s superclasses, Sunspot::DSL::FieldQuery and Sunspot::DSL::Scope

See Sunspot.search for usage examples

Direct Known Subclasses

Search

Constant Summary

Constants inherited from Scope

Scope::NONE

Instance Method Summary collapse

Methods inherited from FieldQuery

#facet, #order_by, #order_by_random

Methods inherited from Scope

#all_of, #any_of, #dynamic, #initialize, #with, #without

Constructor Details

This class inherits a constructor from Sunspot::DSL::Scope

Instance Method Details

#keywords(keywords, options = {}) ⇒ Object

Specify a phrase that should be searched as fulltext. Only text fields are searched - see DSL::Fields.text

Keyword search is executed using Solr’s dismax handler, which strikes a good balance between powerful and foolproof. In particular, well-matched quotation marks can be used to group phrases, and the + and - modifiers work as expected. All other special Solr boolean syntax is escaped, and mismatched quotes are ignored entirely.

Parameters

keywords<String>

phrase to perform fulltext search on

Options

:fields<Array>

List of fields that should be searched for keywords. Defaults to all fields configured for the types under search.



31
32
33
# File 'lib/sunspot/dsl/query.rb', line 31

def keywords(keywords, options = {})
  @query.set_keywords(keywords, options)
end

#paginate(options = {}) ⇒ Object

Paginate your search. This works the same way as WillPaginate’s paginate().

Note that Solr searches are always paginated. Not calling #paginate is the equivalent of calling:

paginate(:page => 1, :per_page => Sunspot.config.pagination.default_per_page)

Options (options)

:page<Integer>

The requested page (required)

:per_page<Integer>

How many results to return per page. The default is the value in Sunspot.config.pagination.default_per_page

Raises:

  • (ArgumentError)


51
52
53
54
55
56
# File 'lib/sunspot/dsl/query.rb', line 51

def paginate(options = {})
  page = options.delete(:page) || raise(ArgumentError, "paginate requires a :page argument")
  per_page = options.delete(:per_page)
  raise ArgumentError, "unknown argument #{options.keys.first.inspect} passed to paginate" unless options.empty?
  @query.paginate(page, per_page)
end