Class: Sunspot::Query::FieldFacet

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

Overview

Encapsulates a query component representing a field facet. Users create instances using DSL::Query#facet

Direct Known Subclasses

DateFieldFacet

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, options) ⇒ FieldFacet

Returns a new instance of FieldFacet.



39
40
41
# File 'lib/sunspot/query/field_facet.rb', line 39

def initialize(field, options)
  @field, @options = field, options
end

Class Method Details

.build(field, options) ⇒ Object

Return the appropriate FieldFacet instance for the field and options. If a :time_range option is specified, and the field type is TimeType, build a DateFieldFacet. Otherwise, build a normal FieldFacet.

Returns

FieldFacet

FieldFacet instance of appropriate class.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sunspot/query/field_facet.rb', line 22

def build(field, options)
  if options.has_key?(:time_range)
    unless field.type == Type::TimeType
      raise(
        ArgumentError,
        ":time_range key can only be specified for time fields"
      )
    end
    DateFieldFacet.new(field, options)
  elsif options.has_key?(:only)
    QueryFieldFacet.new(field, options.delete(:only), options)
  else
    FieldFacet.new(field, options)
  end
end

Instance Method Details

#to_paramsObject

Returns

Hash

solr-ruby params for this field facet



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sunspot/query/field_facet.rb', line 47

def to_params
  params = { :"facet.field" => [@field.indexed_name], :facet => 'true' }
  params[param_key(:sort)] = 
    case @options[:sort]
    when :count then 'true'
    when :index then 'false'
    when nil
    else raise(ArgumentError, 'Allowed facet sort options are :count and :index')
    end
  params[param_key(:limit)] = @options[:limit]
  params[param_key(:mincount)] =
    if @options[:minimum_count] then @options[:minimum_count]
    elsif @options[:zeros] then 0
    else 1
    end
  params
end