Class: Sunspot::DSL::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/dsl/group.rb

Instance Method Summary collapse

Constructor Details

#initialize(setup, group) ⇒ Group

Returns a new instance of Group.



4
5
6
# File 'lib/sunspot/dsl/group.rb', line 4

def initialize(setup, group)
  @setup, @group = setup, group
end

Instance Method Details

#field(*field_names, &block) ⇒ Object

Specify one or more fields for result grouping.

Parameters

field_names…<Symbol>

the fields to use for grouping



14
15
16
17
18
19
# File 'lib/sunspot/dsl/group.rb', line 14

def field(*field_names, &block)
  field_names.each do |field_name|
    field = @setup.field(field_name)
    @group.add_field(field)
  end
end

#limit(num) ⇒ Object

Sets the number of results (documents) to return for each group. Defaults to 1.



38
39
40
# File 'lib/sunspot/dsl/group.rb', line 38

def limit(num)
  @group.limit = num
end

#ngroups(enabled) ⇒ Object

The group.ngroups option true return the total number of groups this is expensive and sometimes you don’t need it! If ngroups is false paginated_collection last_page? and total_pages wont’t work. Defaults to true.



68
69
70
# File 'lib/sunspot/dsl/group.rb', line 68

def ngroups(enabled)
  @group.ngroups = enabled
end

#order_by(field_name, direction = nil) ⇒ Object

Specify the order that results should be returned in. This method can be called multiple times; precedence will be in the order given.

Parameters

field_name<Symbol>

the field to use for ordering

direction<Symbol>

:asc or :desc (default :asc)



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sunspot/dsl/group.rb', line 80

def order_by(field_name, direction = nil)
  sort =
    if special = Sunspot::Query::Sort.special(field_name)
      special.new(direction)
    else
      Sunspot::Query::Sort::FieldSort.new(
        @setup.field(field_name), direction
      )
    end
  @group.add_sort(sort)
end

#order_by_function(*args) ⇒ Object

Specify that results should be ordered based on a FunctionQuery - wiki.apache.org/solr/FunctionQuery Solr 3.1 and up

For example, to order by field1 + (field2*field3):

  order_by_function :sum, :field1, [:product, :field2, :field3], :desc

Parameters

function_name<Symbol>

the function to run

arguments

the arguments for this function.

  • Symbol for a field or function name

  • Array for a nested function

  • String for a literal constant

direction<Symbol>

:asc or :desc



111
112
113
114
115
# File 'lib/sunspot/dsl/group.rb', line 111

def order_by_function(*args)
  @group.add_sort(
    Sunspot::Query::Sort::FunctionSort.new(@setup,args)
  )
end

#query(label, &block) ⇒ Object

Specify a query to group results by.

Parameters

label<Object>

a label for this group; when #value is called on this

group's results, this label will be returned.


28
29
30
31
32
# File 'lib/sunspot/dsl/group.rb', line 28

def query(label, &block)
  group_query = Sunspot::Query::GroupQuery.new(label)
  Sunspot::Util.instance_eval_or_call(Scope.new(group_query, @setup), &block)
  @group.add_query(group_query)
end

#truncateObject

If set, facet counts are based on the most relevant document of each group matching the query.

Supported in Solr 3.4 and above.

Example

Sunspot.search(Post) do
  group :title do
    truncate
  end

  facet :title, :extra => :any
end


58
59
60
# File 'lib/sunspot/dsl/group.rb', line 58

def truncate
  @group.truncate = true
end