Method: Sunspot::DSL::FieldQuery#facet

Defined in:
lib/sunspot/dsl/field_query.rb

#facet(*field_names, &block) ⇒ Object

Request facets on the given field names. If the last argument is a hash, the given options will be applied to all specified fields. See Sunspot::Search#facet and Sunspot::Facet for information on what is returned.

Parameters

field_names…<Symbol>

fields for which to return field facets

Options

:sort<Symbol>

Either :count (values matching the most terms first) or :index (lexical)

:limit<Integer>

The maximum number of facet rows to return

:minimum_count<Integer>

The minimum count a facet row must have to be returned

:zeros<Boolean>

Return facet rows for which there are no matches (equivalent to :minimum_count => 0). Default is false.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sunspot/dsl/field_query.rb', line 62

def facet(*field_names, &block)
  if block
    options =
      if field_names.last.is_a?(Hash)
        field_names.pop
      else
        {}
      end
    if field_names.length != 1
      raise(
        ArgumentError,
        "wrong number of arguments (#{field_names.length} for 1)"
      )
    end
    name = field_names.first
    DSL::QueryFacet.new(@query.add_query_facet(name, options), @setup).instance_eval(&block)
  else
    options = 
      if field_names.last.is_a?(Hash)
        field_names.pop
      else
        {}
      end
    for field_name in field_names
      @query.add_field_facet(@setup.field(field_name), options)
    end
  end
end