Class: Sunspot::Query::CommonQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/query/common_query.rb

Direct Known Subclasses

MoreLikeThisQuery, StandardQuery

Instance Method Summary collapse

Constructor Details

#initialize(types) ⇒ CommonQuery

Returns a new instance of CommonQuery.



4
5
6
7
8
9
10
11
12
13
# File 'lib/sunspot/query/common_query.rb', line 4

def initialize(types)
  @scope = Scope.new
  @sort = SortComposite.new
  @components = [@scope, @sort]
  if types.length == 1
    @scope.add_positive_restriction(TypeField.instance, Restriction::EqualTo, types.first)
  else
    @scope.add_positive_restriction(TypeField.instance, Restriction::AnyOf, types)
  end
end

Instance Method Details

#[](key) ⇒ Object



57
58
59
# File 'lib/sunspot/query/common_query.rb', line 57

def [](key)
  to_params[key.to_sym]
end

#add_field_facet(facet) ⇒ Object



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

def add_field_facet(facet)
  @components << facet
  facet
end

#add_function(function) ⇒ Object



33
34
35
36
# File 'lib/sunspot/query/common_query.rb', line 33

def add_function(function)
  @components << function
  function
end

#add_query_facet(facet) ⇒ Object



28
29
30
31
# File 'lib/sunspot/query/common_query.rb', line 28

def add_query_facet(facet)
  @components << facet
  facet
end

#add_sort(sort) ⇒ Object



19
20
21
# File 'lib/sunspot/query/common_query.rb', line 19

def add_sort(sort)
  @sort << sort
end

#pageObject



61
62
63
# File 'lib/sunspot/query/common_query.rb', line 61

def page
  @pagination.page if @pagination
end

#paginate(page, per_page) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/sunspot/query/common_query.rb', line 38

def paginate(page, per_page)
  if @pagination
    @pagination.page = page
    @pagination.per_page = per_page
  else
    @components << @pagination = Pagination.new(page, per_page)
  end
end

#per_pageObject



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

def per_page
  @pagination.per_page if @pagination
end

#solr_parameter_adjustment=(block) ⇒ Object



15
16
17
# File 'lib/sunspot/query/common_query.rb', line 15

def solr_parameter_adjustment=(block)
  @parameter_adjustment = block
end

#to_paramsObject



47
48
49
50
51
52
53
54
55
# File 'lib/sunspot/query/common_query.rb', line 47

def to_params
  params = {}
  @components.each do |component|
    Sunspot::Util.deep_merge!(params, component.to_params)
  end
  @parameter_adjustment.call(params) if @parameter_adjustment
  params[:q] ||= '*:*'
  params
end