Class: ElasticGraph::GraphQL::Aggregation::DateHistogramGrouping

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb

Overview

Constant Summary collapse

INNER_META =
{
  # On a date histogram aggregation, the `key` is formatted as a number (milliseconds since epoch). We
  # need it formatted as a string, which `key_as_string` provides.
  "key_path" => ["key_as_string"],
  # Date histogram aggregations do not have any doc count error. Our resolver is generic and expects
  # there to always be a `doc_count_error_upper_bound`. So we want to tell it to merge an error of `0`
  # into each bucket.
  "merge_into_bucket" => {"doc_count_error_upper_bound" => 0}
}

Instance Method Summary collapse

Instance Method Details

#composite_clause(grouping_options: {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb', line 29

def composite_clause(grouping_options: {})
  interval_options = INTERVAL_OPTIONS_BY_NAME.fetch(interval) do
    raise ArgumentError, "#{interval.inspect} is an unsupported interval. Valid values: #{INTERVAL_OPTIONS_BY_NAME.keys.inspect}."
  end

  inner_hash = interval_options.merge(grouping_options).merge({
    "field" => encoded_index_field_path,
    "format" => DATASTORE_DATE_TIME_FORMAT,
    "offset" => offset,
    "time_zone" => time_zone
  }.compact)

  {"date_histogram" => inner_hash}
end

#encoded_index_field_pathObject



25
26
27
# File 'lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb', line 25

def encoded_index_field_path
  @encoded_index_field_path ||= FieldPathEncoder.join(field_path.filter_map(&:name_in_index))
end

#inner_metaObject



52
53
54
# File 'lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb', line 52

def inner_meta
  INNER_META
end

#keyObject



21
22
23
# File 'lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb', line 21

def key
  @key ||= FieldPathEncoder.encode(field_path.map(&:name_in_graphql_query))
end

#non_composite_clause_for(query) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/elastic_graph/graphql/aggregation/date_histogram_grouping.rb', line 44

def non_composite_clause_for(query)
  # `min_doc_count: 1` is important so we don't have excess buckets when there is a large gap
  # between document dates. For example, if you group on a field at the year truncation unit, and
  # a one-off rogue document has an incorrect timestamp for hundreds of years ago, you'll wind
  # up with a bucket for each intervening year. `min_doc_count: 1` excludes those empty buckets.
  composite_clause(grouping_options: {"min_doc_count" => 1})
end