Class: Elasticated::DateHistogramAggregation

Inherits:
HistogramAggregation show all
Includes:
Subaggregated
Defined in:
lib/elasticated/aggregations/date_histogram_aggregation.rb

Instance Attribute Summary collapse

Attributes included from Subaggregated

#_subaggregations

Attributes inherited from HistogramAggregation

#interval

Attributes inherited from Aggregation

#alias_name, #extra_params, #field

Instance Method Summary collapse

Methods included from Subaggregated

#initialize_subaggregations

Methods inherited from HistogramAggregation

#default_name

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, opts = {}, &block) ⇒ DateHistogramAggregation

Returns a new instance of DateHistogramAggregation.



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

def initialize(field, opts={}, &block)
  self.format = opts.delete(:format) || 'yyyy-MM-dd'
  interval = opts.delete(:interval) || 'day'
  self.offset = opts.delete(:offset)
  super field, interval, opts, &block
end

Instance Attribute Details

#formatObject

Returns the value of attribute format.



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

def format
  @format
end

#offsetObject

Returns the value of attribute offset.



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

def offset
  @offset
end

Instance Method Details

#buildObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/elasticated/aggregations/date_histogram_aggregation.rb', line 14

def build
  terms = { field: field, interval: interval, format: format }
  if offset # '1.4 style'
    terms.merge! pre_offset: offset
    terms.merge! post_offset: offset
  end
  terms.merge! extra_params
  aggregation_struct = { date_histogram: terms }
  aggregation_struct.merge! build_subaggregations
  aggregation_struct
end

#parse(response) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/elasticated/aggregations/date_histogram_aggregation.rb', line 26

def parse(response)
  response['buckets'].inject({}) do |hash, bucket|
    bucket_hash = { 'count' => bucket['doc_count'] }
    bucket_hash.merge! parse_subaggregations(bucket)
    hash.merge bucket['key_as_string'] => bucket_hash
  end
end