Class: Sunspot::Query

Inherits:
Object
  • Object
show all
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. 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

Instance Method Summary collapse

Constructor Details

#initialize(types, params, configuration) ⇒ Query

Returns a new instance of Query.



12
13
14
15
16
# File 'lib/sunspot/query.rb', line 12

def initialize(types, params, configuration)
  @types, @configuration = types, configuration
  @rows = @configuration.pagination.default_per_page
  apply_params(params)
end

Instance Attribute Details

#keywords=(value) ⇒ Object (writeonly)

:nodoc:



10
11
12
# File 'lib/sunspot/query.rb', line 10

def keywords=(value)
  @keywords = value
end

Instance Method Details

#add_component(component) ⇒ Object

Add a query component

Parameters

component<~to_params>

A restriction query component



53
54
55
# File 'lib/sunspot/query.rb', line 53

def add_component(component)
  components << component
end

#add_field_facet(field_name) ⇒ Object

Add a field facet

Parameters

field_name<Symbol>

Name of the field on which to get a facet



88
89
90
# File 'lib/sunspot/query.rb', line 88

def add_field_facet(field_name)
  add_component(Facets::FieldFacet.new(field(field_name)))
end

#add_restriction(field_name, restriction_clazz, value, negative = false) ⇒ Object

Add instance of Sunspot::Restriction::Base to query components. This method is exposed to the DSL because the Query instance holds field definitions and is able to translate field names into full field definitions, and memoize # the result.

Parameters

field_name<Symbol>

Name of the field to which the restriction applies

restriction_clazz<Class>

Subclass of Sunspot::Restriction::Base to instantiate

value<Object>

Value against which the restriction applies (e.g. less_than(2) has a value of 2)

negative

Whether this restriction should be negated

Returns

Sunspot::Restriction::Base

Restriction instance



77
78
79
# File 'lib/sunspot/query.rb', line 77

def add_restriction(field_name, restriction_clazz, value, negative = false)
  add_component(restriction_clazz.new(field(field_name), value, negative))
end

#dslObject

Get a DSL instance for building this query.

Returns

Sunspot::DSL::Query

DSL instance



156
157
158
# File 'lib/sunspot/query.rb', line 156

def dsl
  @dsl ||= DSL::Query.new(self)
end

#field(field_name) ⇒ Object

Get a Sunspot::Field::Base instance corresponding to the given field name

Parameters

field_name<Symbol>

The field name for which to find a field

Returns

Sunspot::Field::Base

The field object corresponding to the given name

Raises

ArgumentError

If the given field name is not configured for the types being queried



176
177
178
# File 'lib/sunspot/query.rb', line 176

def field(field_name)
  fields_hash[field_name.to_sym] || raise(UnrecognizedFieldError, "No field configured for #{@types * ', '} with name '#{field_name}'")
end

#order_by(field_name, direction = nil) ⇒ Object

Set result ordering.

Parameters

field_name<Symbol>

Name of the field on which to order

direction<Symbol>

:asc or :desc (default :asc)



116
117
118
119
# File 'lib/sunspot/query.rb', line 116

def order_by(field_name, direction = nil)
  direction ||= :asc
  (@sort ||= []) << { field(field_name).indexed_name.to_sym => (direction.to_s == 'asc' ? :ascending : :descending) }
end

#pageObject

Page that this query will return (used by Sunspot::Search to expose pagination)

Returns

Integer

Page number



129
130
131
132
133
134
135
# File 'lib/sunspot/query.rb', line 129

def page
  if @start && @rows
    @start / @rows + 1
  else
    1
  end
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



102
103
104
105
106
# File 'lib/sunspot/query.rb', line 102

def paginate(page, per_page = nil)
  per_page ||= @configuration.pagination.default_per_page
  @start = (page - 1) * per_page
  @rows = per_page
end

#per_pageObject

Number of rows per page that this query will return (used by Sunspot::Search to expose pagination)

Returns

Integer

Rows per page



145
146
147
# File 'lib/sunspot/query.rb', line 145

def per_page
  @rows
end

#to_paramsObject

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sunspot/query.rb', line 31

def to_params
  params = {}
  query_components = []
  query_components << @keywords if @keywords
  query_components << types_phrase if types_phrase
  params[:q] = query_components.map { |component| "(#{component})"} * ' AND '
  params[:sort] = @sort if @sort
  params[:start] = @start if @start
  params[:rows] = @rows if @rows
  for component in components
    Util.deep_merge!(params, component.to_params)
  end
  params
end