Class: Elasticated::SafeDateHistogramAggregation

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

Constant Summary collapse

DEFAULT_INTERVAL =
'1d'

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) ⇒ SafeDateHistogramAggregation

Returns a new instance of SafeDateHistogramAggregation.



9
10
11
12
13
14
15
16
17
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 9

def initialize(field, opts={}, &block)
  self.time_zone = opts.delete(:time_zone)
  self.points    = opts.fetch(:points)
  opts.delete(:points)
  self.compact   = opts.delete(:compact) { false }
  self.include_count = opts.delete(:include_count) { true }
  interval = opts.delete(:interval) || DEFAULT_INTERVAL
  super field, interval, opts, &block
end

Instance Attribute Details

#compactObject

Returns the value of attribute compact.



7
8
9
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 7

def compact
  @compact
end

#include_countObject

Returns the value of attribute include_count.



7
8
9
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 7

def include_count
  @include_count
end

#pointsObject

Returns the value of attribute points.



7
8
9
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 7

def points
  @points
end

#time_zoneObject

Returns the value of attribute time_zone.



7
8
9
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 7

def time_zone
  @time_zone
end

Instance Method Details

#buildObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 19

def build
  terms = { field: field, interval: safe_interval.to_s }
  if time_zone
    terms.merge! time_zone: time_zone
  end
  terms.merge! extra_params
  aggregation_struct = { date_histogram: terms }
  aggregation_struct.merge! build_subaggregations
  aggregation_struct
end

#parse(response) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/elasticated/aggregations/safe_date_histogram_aggregation.rb', line 30

def parse(response)
  response['buckets'].each_with_object({}) do |bucket, hash|
    count = bucket['doc_count']
    key = build_key(bucket)

    if _subaggregations.empty?
      hash[key] ||= 0
      if compact
        hash[key] = hash[key] + count
      else
        hash[key]['count'] ||= 0
        hash[key]['count'] = hash[key]['count'] + count
      end
    else
      parsed_subaggregations = parse_subaggregations(bucket)
      hash[key] = value_for(parsed_subaggregations, hash, key).tap do |h|
        h['count'] = count #if include_count
      end
    end
  end
end