Class: Sunspot::Query::FieldGroup

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

Overview

A FieldGroup groups by the unique values of a given field.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ FieldGroup

Returns a new instance of FieldGroup.



9
10
11
12
13
14
15
16
# File 'lib/sunspot/query/field_group.rb', line 9

def initialize(field)
  if field.multiple?
    raise(ArgumentError, "#{field.name} cannot be used for grouping because it is a multiple-value field")
  end
  @field = field

  @sort = SortComposite.new
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



7
8
9
# File 'lib/sunspot/query/field_group.rb', line 7

def limit
  @limit
end

#truncateObject

Returns the value of attribute truncate.



7
8
9
# File 'lib/sunspot/query/field_group.rb', line 7

def truncate
  @truncate
end

Instance Method Details

#add_sort(sort) ⇒ Object



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

def add_sort(sort)
  @sort << sort
end

#to_paramsObject



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

def to_params
  params = {
    :group            => "true",
    :"group.ngroups"  => "true",
    :"group.field"    => @field.indexed_name
  }

  params.merge!(@sort.to_params("group."))
  params[:"group.limit"] = @limit if @limit
  params[:"group.truncate"] = @truncate if @truncate

  params
end