Class: Sunspot::Query::Query
- Inherits:
-
FieldQuery
- Object
- Scope
- FieldQuery
- Sunspot::Query::Query
- Defined in:
- lib/sunspot/query.rb
Overview
This class encapsulates a query that is to be sent to Solr. The query is constructed in the block passed to the Sunspot.search method, using the Sunspot::DSL::Query interface. It can also be accessed directly by calling #query on a Search object (presumably a not-yet-run one created using Sunspot#new_search), which might be more suitable than the DSL when an intermediate object has responsibility for building the query dynamically. – Instances of Query, as well as all of the components it contains, respond to the #to_params method, which returns a hash of parameters in the format recognized by the solr-ruby API.
Instance Attribute Summary collapse
-
#query_facets ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#add_component(component) ⇒ Object
Add a component to the query.
-
#add_sort(sort) ⇒ Object
Add a Sort object into this query’s sort composite.
-
#initialize(types, setup, configuration) ⇒ Query
constructor
:nodoc:.
-
#keywords=(keywords) ⇒ Object
Set the keywords for this query.
-
#options=(options) ⇒ Object
Pass in search options as a hash.
-
#order_by_random ⇒ Object
Add random ordering to the search.
-
#page ⇒ Object
Page that this query will return (used by Sunspot::Search to expose pagination).
-
#paginate(page, per_page = nil) ⇒ Object
Sets @start and @rows instance variables using pagination semantics.
-
#per_page ⇒ Object
Number of rows per page that this query will return (used by Sunspot::Search to expose pagination).
-
#query_facet(name) ⇒ Object
Get the query facet with the given name.
-
#set_keywords(keywords, options = {}) ⇒ Object
Set the keywords for this query, along with keyword options.
-
#to_params ⇒ Object
Representation of this query as solr-ruby parameters.
Methods inherited from FieldQuery
#add_field_facet, #add_query_facet, #order_by
Methods inherited from Scope
#add_conjunction, #add_disjunction, #add_negated_restriction, #add_negated_shorthand_restriction, #add_restriction, #add_shorthand_restriction, #dynamic_query, #exclude_instance
Constructor Details
#initialize(types, setup, configuration) ⇒ Query
:nodoc:
24 25 26 27 28 29 30 31 |
# File 'lib/sunspot/query.rb', line 24 def initialize(types, setup, configuration) #:nodoc: @setup = setup @components = [] @query_facets = {} @components << @base_query = BaseQuery.new(types, setup) @components << @pagination = Pagination.new(configuration) @components << @sort = SortComposite.new end |
Instance Attribute Details
#query_facets ⇒ Object (readonly)
:nodoc:
22 23 24 |
# File 'lib/sunspot/query.rb', line 22 def query_facets @query_facets end |
Instance Method Details
#add_component(component) ⇒ Object
Add a component to the query. Used by objects that proxy to the query object.
Parameters
- component<~to_params>
-
Query component to add.
49 50 51 |
# File 'lib/sunspot/query.rb', line 49 def add_component(component) #:nodoc: @components << component end |
#add_sort(sort) ⇒ Object
Add a Sort object into this query’s sort composite.
132 133 134 |
# File 'lib/sunspot/query.rb', line 132 def add_sort(sort) #:nodoc: @sort << sort end |
#keywords=(keywords) ⇒ Object
Set the keywords for this query. Keywords are parsed with Solr’s dismax handler.
37 38 39 |
# File 'lib/sunspot/query.rb', line 37 def keywords=(keywords) set_keywords(keywords) end |
#options=(options) ⇒ Object
Pass in search options as a hash. This is not the preferred way of building a Sunspot search, but it is made available as experience shows Ruby developers like to pass in hashes. Probably nice for quick one-offs on the console, anyway.
Options (options
)
- :keywords
-
Keyword string for fulltext search
- :conditions
-
Hash of key-value pairs, where keys are field names, and values are one of scalar, Array, or Range. Scalars are evaluated as EqualTo restrictions; Arrays are AnyOf restrictions, and Ranges are Between restrictions.
- :order
-
Order the search results. Either a string or array of strings of the form “field_name direction”
- :page
-
Page to use for pagination
- :per_page
-
Number of results to show per page
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/sunspot/query.rb', line 167 def () #:nodoc: if .has_key?(:keywords) self.keywords = [:keywords] end if .has_key?(:conditions) [:conditions].each_pair do |field_name, value| begin add_shorthand_restriction(field_name, value) rescue UnrecognizedFieldError # ignore fields we don't recognize end end end if .has_key?(:order) for order in Array([:order]) order_by(*order.split(' ')) end end if .has_key?(:page) paginate([:page], [:per_page]) end end |
#order_by_random ⇒ Object
Add random ordering to the search. This can be added after other field-based sorts if desired.
71 72 73 |
# File 'lib/sunspot/query.rb', line 71 def order_by_random add_sort(Sort.new(RandomField.new)) end |
#page ⇒ Object
Page that this query will return (used by Sunspot::Search to expose pagination)
Returns
- Integer
-
Page number
105 106 107 |
# File 'lib/sunspot/query.rb', line 105 def page #:nodoc: @pagination.page end |
#paginate(page, per_page = nil) ⇒ Object
Sets @start and @rows instance variables using pagination semantics
Parameters
- page<Integer>
-
Page on which to start
- per_page<Integer>
-
How many rows to display per page. Default taken from Sunspot.config.pagination.default_per_page
63 64 65 |
# File 'lib/sunspot/query.rb', line 63 def paginate(page, per_page = nil) @pagination.page, @pagination.per_page = page, per_page end |
#per_page ⇒ Object
Number of rows per page that this query will return (used by Sunspot::Search to expose pagination)
Returns
- Integer
-
Rows per page
117 118 119 |
# File 'lib/sunspot/query.rb', line 117 def per_page #:nodoc: @pagination.per_page end |
#query_facet(name) ⇒ Object
Get the query facet with the given name. Used by the Search object to match query facet results with the requested query facets.
125 126 127 |
# File 'lib/sunspot/query.rb', line 125 def query_facet(name) #:nodoc: @query_facets[name.to_sym] end |
#set_keywords(keywords, options = {}) ⇒ Object
Set the keywords for this query, along with keyword options. See Query::BaseQuery for information on what the options do.
140 141 142 143 |
# File 'lib/sunspot/query.rb', line 140 def set_keywords(keywords, = {}) #:nodoc: @base_query.keywords = keywords @base_query. = end |
#to_params ⇒ Object
Representation of this query as solr-ruby parameters. Constructs the hash by deep-merging scope and facet parameters, adding in various other parameters from instance data.
Note that solr-ruby takes the :q parameter as a separate argument; for the sake of consistency, the Query object ignores this fact (the Search object extracts it back out).
Returns
- Hash
-
Representation of query in solr-ruby form
88 89 90 91 92 93 94 95 |
# File 'lib/sunspot/query.rb', line 88 def to_params #:nodoc: params = {} query_components = [] for component in @components Util.deep_merge!(params, component.to_params) end params end |