class Elasticband::Aggregation

Constants

PARSE_AGGREGATIONS

Attributes

name[RW]

Public Class Methods

merge(*aggregations) click to toggle source
# File lib/elasticband/aggregation.rb, line 55
def merge(*aggregations)
  aggregations.each_with_object({}) { |a, h| h.merge!(a.to_h) }
end
new(name) click to toggle source
# File lib/elasticband/aggregation.rb, line 14
def initialize(name)
  self.name = name.to_s.gsub(/\W/, '_'.freeze).to_sym
end
parse(options) click to toggle source

Parses some options to a Elasticsearch syntax, aggregations can be nested in another.

#### Options

#### Examples “‘ Aggregation.parse(group_by: :status)

> { status: { terms: { field: :status } } }

Aggregation.parse(group_max: :contents_count)

> { status: { max: { field: :contents_count } } }

Aggregation.parse(group_by: [:status, size: 5, top_hits: 3])

> { status: { terms: { field: :status, size: 5 }, aggs: { top_status: { top_hits: 3 } } } }

Aggregation.parse(group_by_filter: [:published_results, only: { status: :published }])

> { published_results: { filter: { term: { status: :published } } } }

“‘

# File lib/elasticband/aggregation.rb, line 51
def parse(options)
  merge(*parse_aggregations(options))
end

Private Class Methods

parse_aggregations(options, root_aggregation = nil) click to toggle source
# File lib/elasticband/aggregation.rb, line 61
def parse_aggregations(options, root_aggregation = nil)
  parse_options = options.slice(*PARSE_AGGREGATIONS)
  return root_aggregation if parse_options.blank?

  aggregations = parse_options.map do |aggregation_name, aggregation_options|
    parse_singular_aggregation(root_aggregation, aggregation_name, aggregation_options)
  end

  aggregations = Aggregation::Nested.new(root_aggregation, aggregations) if root_aggregation
  aggregations
end
parse_field_aggregation(aggregation_class, prefix, options_aggregation) click to toggle source
# File lib/elasticband/aggregation.rb, line 82
def parse_field_aggregation(aggregation_class, prefix, options_aggregation)
  return {} if options_aggregation.blank?

  field, options = options_aggregation
  options ||= {}

  name = :"#{prefix}_#{field}"
  aggregation = aggregation_class.new(name, field, options.except(*PARSE_AGGREGATIONS))
  parse_aggregations(options, aggregation)
end
parse_filter(aggregation_name, options) click to toggle source
# File lib/elasticband/aggregation.rb, line 104
def parse_filter(aggregation_name, options)
  return {} if options.blank?

  filter = Elasticband::Filter.parse(options)
  filter_options = options.except(*(PARSE_AGGREGATIONS + Elasticband::Filter::PARSE_FILTERS))
  aggregation = Aggregation::Filter.new(aggregation_name, filter, filter_options)
  parse_aggregations(options, aggregation)
end
parse_singular_aggregation(root_aggregation, aggregation_name, aggregation_options) click to toggle source
# File lib/elasticband/aggregation.rb, line 73
def parse_singular_aggregation(root_aggregation, aggregation_name, aggregation_options)
  case aggregation_name
  when :group_by then parse_field_aggregation(Aggregation::Terms, :by, aggregation_options)
  when :group_max then parse_field_aggregation(Aggregation::Max, :max, aggregation_options)
  when :top_hits then parse_top_hits(root_aggregation, aggregation_options)
  when :group_by_filter then parse_filter(*aggregation_options)
  end
end
parse_top_hits(root_aggregation, options_top_hits) click to toggle source
# File lib/elasticband/aggregation.rb, line 93
def parse_top_hits(root_aggregation, options_top_hits)
  return {} if options_top_hits.blank?

  size, options = options_top_hits
  options ||= {}

  name = :"top_#{root_aggregation.name}"
  aggregation = Aggregation::TopHits.new(name, size, options.except(*PARSE_AGGREGATIONS))
  parse_aggregations(options, aggregation)
end

Public Instance Methods

to_h(aggregation_hash = {}) click to toggle source
# File lib/elasticband/aggregation.rb, line 18
def to_h(aggregation_hash = {})
  { name => aggregation_hash }
end