Class: Sunspot::DSL::Query
- Inherits:
-
FieldQuery
- Object
- Scope
- FieldQuery
- Sunspot::DSL::Query
- 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
Constant Summary
Constants inherited from Scope
Instance Method Summary collapse
-
#keywords(keywords, options = {}, &block) ⇒ Object
Specify a phrase that should be searched as fulltext.
-
#near(coordinates, miles) ⇒ Object
Scope the search by geographical distance from a given point.
-
#paginate(options = {}) ⇒ Object
Paginate your search.
-
#text_fields(&block) ⇒ Object
Apply scope-type restrictions on fulltext fields.
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 = {}, &block) ⇒ 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.
This method can optionally take a block, which is evaluated by the Fulltext DSL class, and exposes several powerful dismax features.
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.
- :highlight<Boolean,Array>
-
If true, perform keyword highlighting on all searched fields. If an array of field names, perform highlighting on the specified fields. This can also be called from within the fulltext block.
38 39 40 41 42 43 44 45 46 |
# File 'lib/sunspot/dsl/query.rb', line 38 def keywords(keywords, = {}, &block) fulltext_base_query = @query.set_keywords(keywords, ) if block && fulltext_base_query Util.instance_eval_or_call( Fulltext.new(fulltext_base_query), &block ) end end |
#near(coordinates, miles) ⇒ Object
Scope the search by geographical distance from a given point. coordinates
should either respond to #first and #last (e.g. a two-element array), or to #lat and one of #lng, #lon, or #long. miles
is the radius around the point for which to return documents.
77 78 79 |
# File 'lib/sunspot/dsl/query.rb', line 77 def near(coordinates, miles) @query.add_location_restriction(coordinates, miles) 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,String>
-
The requested page. The default is 1.
- :per_page<Integer,String>
-
How many results to return per page. The default is the value in
Sunspot.config.pagination.default_per_page
64 65 66 67 68 69 |
# File 'lib/sunspot/dsl/query.rb', line 64 def paginate( = {}) page = .delete(:page) per_page = .delete(:per_page) raise ArgumentError, "unknown argument #{.keys.first.inspect} passed to paginate" unless .empty? @query.paginate(page, per_page) end |
#text_fields(&block) ⇒ Object
Apply scope-type restrictions on fulltext fields. In certain situations, it may be desirable to place logical restrictions on text fields. Remember that text fields are tokenized; your mileage may very.
The block works exactly like a normal scope, except that the field names refer to text fields instead of attribute fields.
Example
Sunspot.search(Post) do
text_fields do
with :body, nil
end
end
This will return all documents that do not have a body.
99 100 101 102 103 104 |
# File 'lib/sunspot/dsl/query.rb', line 99 def text_fields(&block) Sunspot::Util.instance_eval_or_call( Scope.new(@query.add_text_fields_scope), &block ) end |