Class: Sunspot::DSL::Query
- Inherits:
-
Object
- Object
- 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.
See Sunspot.search for usage examples
Constant Summary collapse
- NONE =
Object.new
Instance Method Summary collapse
-
#facet(*field_names) ⇒ Object
Request facets on the given field names.
-
#initialize(query) ⇒ Query
constructor
:nodoc:.
-
#keywords(keywords) ⇒ Object
Specify a phrase that should be searched as fulltext.
-
#order_by(field_name, direction = nil) ⇒ Object
Specify the order that results should be returned in.
-
#paginate(options = {}) ⇒ Object
Paginate your search.
-
#with(field_name, value = NONE) ⇒ Object
Build a positive restriction.
-
#without(*args) ⇒ Object
Build a negative restriction (exclusion).
Constructor Details
#initialize(query) ⇒ Query
:nodoc:
13 14 15 |
# File 'lib/sunspot/dsl/query.rb', line 13 def initialize(query) #:nodoc: @query = query end |
Instance Method Details
#facet(*field_names) ⇒ Object
Request facets on the given field names. See Sunspot::Search#facet and Sunspot::Facet for information on what is returned.
Parameters
- field_names…<Symbol>
-
fields for which to return field facets
169 170 171 172 173 |
# File 'lib/sunspot/dsl/query.rb', line 169 def facet(*field_names) for field_name in field_names @query.add_field_facet(field_name) end end |
#keywords(keywords) ⇒ Object
Specify a phrase that should be searched as fulltext. Only text fields are searched - see DSL::Fields.text
Note that the keywords are passed directly to Solr unadulterated. The advantage of this is that users can potentially use boolean logic to make advanced searches. The disadvantage is that syntax errors are possible. This may get better in a future version; suggestions are welcome.
Parameters
- keywords<String>
-
phrase to perform fulltext search on
30 31 32 |
# File 'lib/sunspot/dsl/query.rb', line 30 def keywords(keywords) @query.keywords = keywords end |
#order_by(field_name, direction = nil) ⇒ Object
Specify the order that results should be returned in. This method can be called multiple times; precedence will be in the order given.
Parameters
- field_name<Symbol>
-
the field to use for ordering
- direction<Symbol>
-
:asc or :desc (default :asc)
159 160 161 |
# File 'lib/sunspot/dsl/query.rb', line 159 def order_by(field_name, direction = nil) @query.order_by(field_name, direction) 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
144 145 146 147 148 149 |
# File 'lib/sunspot/dsl/query.rb', line 144 def paginate( = {}) page = .delete(:page) || raise(ArgumentError, "paginate requires a :page argument") per_page = .delete(:per_page) raise ArgumentError, "unknown argument #{.keys.first.inspect} passed to paginate" unless .empty? @query.paginate(page, per_page) end |
#with(field_name, value = NONE) ⇒ Object
Build a positive restriction. With one argument, this method returns another DSL object which presents methods for attaching various restriction types. With two arguments, acts as a shorthand for creating an equality restriction.
Parameters
- field_name<Symbol>
-
Name of the field on which to place the restriction
- value<Symbol>
-
If passed, creates an equality restriction with this value
Returns
- Sunspot::DSL::Restriction
-
Restriction DSL object (if only one argument is passed)
Examples
An equality restriction:
Sunspot.search do
with(:blog_id, 1)
end
Other restriction types:
Sunspot.search(Post) do
with(:average_rating).greater_than(3.0)
end
65 66 67 68 69 70 71 |
# File 'lib/sunspot/dsl/query.rb', line 65 def with(field_name, value = NONE) if value == NONE DSL::Restriction.new(field_name.to_sym, @query, false) else @query.add_restriction(field_name, Sunspot::Restriction::EqualTo, value, false) end end |
#without(*args) ⇒ Object
Build a negative restriction (exclusion). This method can take three forms: equality exclusion, exclusion by another restriction, or identity exclusion. The first two forms work the same way as the #with method; the third excludes a specific instance from the search results.
Parameters (exclusion by field value)
- field_name<Symbol>
-
Name of the field on which to place the exclusion
- value<Symbol>
-
If passed, creates an equality exclusion with this value
Parameters (exclusion by identity)
- args<Object>…
-
One or more instances that should be excluded from the results
Examples
An equality exclusion:
Sunspot.search(Post) do
without(:blog_id, 1)
end
Other restriction types:
Sunspot.search(Post) do
without(:average_rating).greater_than(3.0)
end
Exclusion by identity:
Sunspot.search(Post) do
without(some_post_instance)
end
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sunspot/dsl/query.rb', line 110 def without(*args) case args.first when String, Symbol field_name = args[0] value = args.length > 1 ? args[1] : NONE if value == NONE DSL::Restriction.new(field_name.to_sym, @query, true) else @query.add_restriction(field_name, Sunspot::Restriction::EqualTo, value, true) end else instances = args for instance in instances.flatten @query.add_component(Sunspot::Restriction::SameAs.new(instance, true)) end end end |