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_adjustments = []
end

Instance Method Details

#[](key) ⇒ Object



93
94
95
# File 'lib/sunspot/query/common_query.rb', line 93

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

#add_field_facet(facet) ⇒ Object



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

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

#add_field_list(field_list) ⇒ Object



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

def add_field_list(field_list)
  @components << field_list
  field_list
end

#add_function(function) ⇒ Object



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

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

#add_geo(geo) ⇒ Object



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

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

#add_group(group) ⇒ Object



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

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

#add_parameter_adjustment(block) ⇒ Object



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

def add_parameter_adjustment(block)
  @parameter_adjustments << block
end

#add_query_facet(facet) ⇒ Object



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

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_spellcheck(options = {}) ⇒ Object



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

def add_spellcheck(options = {})
  @components << Spellcheck.new(options)
end

#add_stats(stats) ⇒ Object



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

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

#cursorObject



105
106
107
# File 'lib/sunspot/query/common_query.rb', line 105

def cursor
  @pagination.cursor if @pagination
end

#pageObject



97
98
99
# File 'lib/sunspot/query/common_query.rb', line 97

def page
  @pagination.page if @pagination
end

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



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

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

  # cursor pagination requires a sort containing a uniqueKey field
  add_sort(Sunspot::Query::Sort.special(:solr_id).new('asc')) if cursor and !@sort.include?('id ')
end

#per_pageObject



101
102
103
# File 'lib/sunspot/query/common_query.rb', line 101

def per_page
  @pagination.per_page if @pagination
end

#to_paramsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sunspot/query/common_query.rb', line 79

def to_params
  params = {}
  @components.each do |component|
    Sunspot::Util.deep_merge!(params, component.to_params)
  end

  @parameter_adjustments.each do |_block|
    _block.call(params)
  end

  params[:q] ||= '*:*'
  params
end