Class: Sunspot::Query::Query

Inherits:
FieldQuery 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. 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

Instance Method Summary collapse

Methods inherited from FieldQuery

#add_field_facet, #add_query_facet, #order_by

Methods inherited from Scope

#add_component, #add_conjunction, #add_disjunction, #add_negated_restriction, #add_negated_shorthand_restriction, #add_restriction, #add_shorthand_restriction, #dynamic_query, #exclude_instance, #to_params

Constructor Details

#initialize(types, setup, configuration) ⇒ Query

:nodoc:



25
26
27
28
29
30
31
# File 'lib/sunspot/query.rb', line 25

def initialize(types, setup, configuration) #:nodoc:
  super(setup)
  @query_facets = {}
  @components[0] = @base_query = BaseQuery.new(types, setup)
  @components << @pagination = Pagination.new(configuration)
  @components << @sort = SortComposite.new
end

Instance Attribute Details

#query_facetsObject (readonly)

:nodoc:



23
24
25
# File 'lib/sunspot/query.rb', line 23

def query_facets
  @query_facets
end

Instance Method Details

#add_location_restriction(coordinates, miles) ⇒ Object

:nodoc:



47
48
49
# File 'lib/sunspot/query.rb', line 47

def add_location_restriction(coordinates, miles) #:nodoc:
  @components << Local.new(coordinates, miles)
end

#add_sort(sort) ⇒ Object

Add a Sort object into this query’s sort composite.



91
92
93
# File 'lib/sunspot/query.rb', line 91

def add_sort(sort) #:nodoc:
  @sort << sort
end

#add_text_fields_scopeObject



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

def add_text_fields_scope
  @components << scope = Scope.new(TextFieldSetup.new(@setup))
  scope
end

#pageObject

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

Returns

Integer

Page number



64
65
66
# File 'lib/sunspot/query.rb', line 64

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



43
44
45
# File 'lib/sunspot/query.rb', line 43

def paginate(page, per_page = nil) #:nodoc:
  @pagination.page, @pagination.per_page = page, 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



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

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.



84
85
86
# File 'lib/sunspot/query.rb', line 84

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::FulltextBaseQuery for information on what the options do. Returns a FulltextBaseQuery object.



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

def set_keywords(keywords, options = {}) #:nodoc:
  if keywords.to_s =~ /\S/
    @components[0] = @base_query =
      FulltextBaseQuery.new(keywords, options, @base_query.types, @setup)
  end
end