class Sunspot::Query::CommonQuery

Public Class Methods

new(types) click to toggle source
# 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

Public Instance Methods

[](key) click to toggle source
# File lib/sunspot/query/common_query.rb, line 93
def [](key)
  to_params[key.to_sym]
end
add_field_facet(facet) click to toggle source
# File lib/sunspot/query/common_query.rb, line 36
def add_field_facet(facet)
  @components << facet
  facet
end
add_field_list(field_list) click to toggle source
# File lib/sunspot/query/common_query.rb, line 26
def add_field_list(field_list)
  @components << field_list
  field_list
end
add_function(function) click to toggle source
# File lib/sunspot/query/common_query.rb, line 46
def add_function(function)
  @components << function
  function
end
add_geo(geo) click to toggle source
# File lib/sunspot/query/common_query.rb, line 51
def add_geo(geo)
  @components << geo
  geo
end
add_group(group) click to toggle source
# File lib/sunspot/query/common_query.rb, line 31
def add_group(group)
  @components << group
  group
end
add_parameter_adjustment(block) click to toggle source
# File lib/sunspot/query/common_query.rb, line 18
def add_parameter_adjustment(block)
  @parameter_adjustments << block
end
add_query_facet(facet) click to toggle source
# File lib/sunspot/query/common_query.rb, line 41
def add_query_facet(facet)
  @components << facet
  facet
end
add_sort(sort) click to toggle source
# File lib/sunspot/query/common_query.rb, line 22
def add_sort(sort)
  @sort << sort
end
add_spellcheck(options = {}) click to toggle source
# File lib/sunspot/query/common_query.rb, line 61
def add_spellcheck(options = {})
  @components << Spellcheck.new(options)
end
add_stats(stats) click to toggle source
# File lib/sunspot/query/common_query.rb, line 56
def add_stats(stats)
  @components << stats
  stats
end
cursor() click to toggle source
# File lib/sunspot/query/common_query.rb, line 105
def cursor
  @pagination.cursor if @pagination
end
page() click to toggle source
# File lib/sunspot/query/common_query.rb, line 97
def page
  @pagination.page if @pagination
end
paginate(page, per_page, offset = nil, cursor = nil) click to toggle source
# 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_page() click to toggle source
# File lib/sunspot/query/common_query.rb, line 101
def per_page
  @pagination.per_page if @pagination
end
to_params() click to toggle source
# 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

Private Instance Methods

merge_fulltext(params) click to toggle source

If we have a single fulltext query, merge is normally. If there are multiple nested queries, serialize them as `query` subqueries.

# File lib/sunspot/query/common_query.rb, line 115
def merge_fulltext(params)
  return nil if @fulltexts.empty?
  return Sunspot::Util.deep_merge!(params, @fulltexts.first.to_params) if @fulltexts.length == 1
  subqueries = @fulltexts.map {|fulltext| fulltext.to_subquery }.join(' ')
  Sunspot::Util.deep_merge!(params, {:q => subqueries})
end