class Solr::Query::Request::Facet

Constants

QUERY_TYPE
RANGE_TYPE
TERMS_TYPE

Attributes

domain[R]
field[R]
filters[R]
gap[R]
limit[R]
lower_bound[R]
name[R]
subfacets[R]
type[R]
upper_bound[R]
value[R]

Public Class Methods

new(field:, type:, name: nil, value: nil, filters: [], subfacets: [], options: {}) click to toggle source
# File lib/solr/query/request/facet.rb, line 23
def initialize(field:, type:, name: nil, value: nil, filters: [], subfacets: [], options: {})
  if options[:limit].nil? && type == TERMS_TYPE
    raise ArgumentError, "Need to specify :limit option value for 'terms' facet type"
  end

  if options.values_at(:gap, :lower_bound, :upper_bound).any?(&:nil?) && type == RANGE_TYPE
    raise ArgumentError, "Need to specify :lower_bound, :upper_bound, :gap option values for 'range' facet type"
  end

  @field       = field
  @name        = name || field
  @type        = type
  @value       = value
  @filters     = filters
  @subfacets   = subfacets
  @limit       = options[:limit].to_i
  @gap         = options[:gap]
  @lower_bound = options[:lower_bound]
  @upper_bound = options[:upper_bound]
  @domain      = options[:domain]
end

Public Instance Methods

to_solr_h() click to toggle source
# File lib/solr/query/request/facet.rb, line 45
def to_solr_h
  return { name.to_s => value } if type == QUERY_TYPE && value

  default_solr_h
end

Protected Instance Methods

default_solr_h() click to toggle source
# File lib/solr/query/request/facet.rb, line 53
def default_solr_h
  {
    "#{name}": {
      type: type,
      field: solarize_field(field),
      limit: limit,
      q: filters.any? ? filters.map(&:to_solr_s).join(' AND ') : nil,
      facet: subfacets.map(&:to_solr_h).reduce(&:merge),
      gap: gap,
      start: lower_bound,
      end: upper_bound,
      domain: domain
    }.compact
  }
end