Class: Elasticated::GroupAggregation

Inherits:
TermsAggregation show all
Includes:
Subaggregated
Defined in:
lib/elasticated/aggregations/group_aggregation.rb

Direct Known Subclasses

SumDistinctAggregation

Instance Attribute Summary collapse

Attributes included from Subaggregated

#_subaggregations

Attributes inherited from TermsAggregation

#order_field, #order_method, #size

Attributes inherited from Aggregation

#alias_name, #extra_params, #field

Instance Method Summary collapse

Methods included from Subaggregated

#initialize_subaggregations

Methods inherited from Aggregation

#name, #original_name

Methods included from Mixins::Inspectionable

#inspect, #text_for_inspect

Methods included from Mixins::BlockEvaluation

#evaluate

Methods included from Mixins::Clonable

#==, #clone

Constructor Details

#initialize(field, *args, &block) ⇒ GroupAggregation

Returns a new instance of GroupAggregation.



7
8
9
10
11
12
13
# File 'lib/elasticated/aggregations/group_aggregation.rb', line 7

def initialize(field, *args, &block)
  super
  self.compact       = extra_params.delete(:compact) { false }
  self.key_as_string = extra_params.delete(:key_as_string) { false }
  raise "The 'include_count' parameter was removed in group aggregations" if extra_params.has_key? :include_count
  initialize_subaggregations &block
end

Instance Attribute Details

#compactObject

Returns the value of attribute compact.



5
6
7
# File 'lib/elasticated/aggregations/group_aggregation.rb', line 5

def compact
  @compact
end

#key_as_stringObject

Returns the value of attribute key_as_string.



5
6
7
# File 'lib/elasticated/aggregations/group_aggregation.rb', line 5

def key_as_string
  @key_as_string
end

Instance Method Details

#buildObject



19
20
21
# File 'lib/elasticated/aggregations/group_aggregation.rb', line 19

def build
  super.merge build_subaggregations
end

#default_nameObject



15
16
17
# File 'lib/elasticated/aggregations/group_aggregation.rb', line 15

def default_name
  "group_by_#{field}"
end

#parse(response) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/elasticated/aggregations/group_aggregation.rb', line 23

def parse(response)
  response['buckets'].each_with_object({}) do |bucket, hash|
    key = bucket['key']
    count = bucket['doc_count']
    key_as_str = bucket['key_as_string']
    hash[key] = if compact && _subaggregations.empty?
      count
    else
      raise "Compact is not a valid parameter on a subaggregated 'terms' aggregation" if compact
      body = { 'count' => count }
      body.merge! 'key_as_string' => key_as_str if key_as_string
      body.merge! parse_subaggregations bucket
      body
    end
  end
end