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
14
15
16
# 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

  @pagination = nil
  @parameter_adjustment = nil
end

Instance Method Details

#[](key) ⇒ Object



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

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

#add_field_facet(facet) ⇒ Object



31
32
33
34
# File 'lib/sunspot/query/common_query.rb', line 31

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

#add_function(function) ⇒ Object



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

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

#add_geo(geo) ⇒ Object



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

def add_geo(geo)
  @components << geo
  geo
end

#add_group(group) ⇒ Object



26
27
28
29
# File 'lib/sunspot/query/common_query.rb', line 26

def add_group(group)
  @components << group
  group
end

#add_query_facet(facet) ⇒ Object



36
37
38
39
# File 'lib/sunspot/query/common_query.rb', line 36

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

#add_sort(sort) ⇒ Object



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

def add_sort(sort)
  @sort << sort
end

#add_stats(stats) ⇒ Object



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

def add_stats(stats)
  @components << stats
  stats
end

#pageObject



80
81
82
# File 'lib/sunspot/query/common_query.rb', line 80

def page
  @pagination.page if @pagination
end

#paginate(page, per_page, offset = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/sunspot/query/common_query.rb', line 56

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

#per_pageObject



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

def per_page
  @pagination.per_page if @pagination
end

#solr_parameter_adjustment=(block) ⇒ Object



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

def solr_parameter_adjustment=(block)
  @parameter_adjustment = block
end

#to_paramsObject



66
67
68
69
70
71
72
73
74
# File 'lib/sunspot/query/common_query.rb', line 66

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