Class: Sunspot::Query::CommonQuery

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

Direct Known Subclasses

MoreLikeThisQuery, StandardQuery

Instance Method Summary (collapse)

Constructor Details

- (CommonQuery) initialize(types)

A new instance of CommonQuery



4
5
6
7
8
9
10
11
12
13
# File 'sunspot/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

- (Object) [](key)



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

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

- (Object) add_field_facet(facet)



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

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

- (Object) add_function(function)



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

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

- (Object) add_query_facet(facet)



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

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

- (Object) add_sort(sort)



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

def add_sort(sort)
  @sort << sort
end

- (Object) page



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

def page
  @pagination.page if @pagination
end

- (Object) paginate(page, per_page, offset = nil)



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

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

- (Object) per_page



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

def per_page
  @pagination.per_page if @pagination
end

- (Object) solr_parameter_adjustment=(block)



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

def solr_parameter_adjustment=(block)
  @parameter_adjustment = block
end

- (Object) to_params



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

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