Class: Elasticband::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticband/aggregation.rb,
lib/elasticband/aggregation/max.rb,
lib/elasticband/aggregation/terms.rb,
lib/elasticband/aggregation/filter.rb,
lib/elasticband/aggregation/nested.rb,
lib/elasticband/aggregation/top_hits.rb,
lib/elasticband/aggregation/field_based.rb

Direct Known Subclasses

FieldBased, Filter, Nested, TopHits

Defined Under Namespace

Classes: FieldBased, Filter, Max, Nested, Terms, TopHits

Constant Summary collapse

PARSE_AGGREGATIONS =
%i(group_by group_max top_hits group_by_filter).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Aggregation

Returns a new instance of Aggregation.



14
15
16
# File 'lib/elasticband/aggregation.rb', line 14

def initialize(name)
  self.name = name.to_s.gsub(/\W/, '_'.freeze).to_sym
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/elasticband/aggregation.rb', line 12

def name
  @name
end

Class Method Details

.merge(*aggregations) ⇒ Object



55
56
57
# File 'lib/elasticband/aggregation.rb', line 55

def merge(*aggregations)
  aggregations.each_with_object({}) { |a, h| h.merge!(a.to_h) }
end

.parse(options) ⇒ Object

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

#### Options

  • ‘group_by:` Count results by the value of an attribute using `terms` filter. It can receive an array with some attributes:

  • ‘group_max:` Group results by maximum value of a field It can receive an array with some attributes:

    • ‘script:` Generates max defined by the script

  • ‘top_hits:` A number of results that should be return inside the group ranked by score.

  • ‘group_by_filter:` Filter and group results with a name using Elasticband::Filter.parse 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 } } } }

“‘



51
52
53
# File 'lib/elasticband/aggregation.rb', line 51

def parse(options)
  merge(*parse_aggregations(options))
end

Instance Method Details

#to_h(aggregation_hash = {}) ⇒ Object



18
19
20
# File 'lib/elasticband/aggregation.rb', line 18

def to_h(aggregation_hash = {})
  { name => aggregation_hash }
end