Class: JayAPI::Elasticsearch::QueryBuilder::Aggregations

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jay_api/elasticsearch/query_builder/aggregations.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/avg.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/max.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/sum.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/errors.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/filter.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/top_hits.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/composite.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/aggregation.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/cardinality.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/value_count.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/sources/terms.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/date_histogram.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/bucket_selector.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/scripted_metric.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/sources/sources.rb,
lib/jay_api/elasticsearch/query_builder/aggregations/errors/aggregations_error.rb

Overview

The list of aggregations to be included in an Elasticsearch query.

Defined Under Namespace

Modules: Errors, Sources Classes: Aggregation, Avg, BucketSelector, Cardinality, Composite, DateHistogram, Filter, Max, ScriptedMetric, Sum, Terms, TopHits, ValueCount

Instance Method Summary collapse

Constructor Details

#initializeAggregations

Returns a new instance of Aggregations.



29
30
31
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 29

def initialize
  @aggregations = []
end

Instance Method Details

#avg(name, field:, missing: nil) ⇒ Object

Adds an avg type aggregation. For information about the parameters



43
44
45
46
47
48
49
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 43

def avg(name, field:, missing: nil)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Avg.new(
      name, field: field, missing: missing
    )
  )
end

#bucket_selector(name, buckets_path:, script:, gap_policy: nil) ⇒ Object

Adds an bucket_selector type aggregation. For information about the parameters



90
91
92
93
94
95
96
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 90

def bucket_selector(name, buckets_path:, script:, gap_policy: nil)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::BucketSelector.new(
      name, buckets_path:, script:, gap_policy:
    )
  )
end

#cardinality(name, field:) ⇒ Object

Adds a cardinality type aggregation. For more information about the parameters



116
117
118
119
120
121
122
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 116

def cardinality(name, field:)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Cardinality.new(
      name, field: field
    )
  )
end

#cloneself

Returns A copy of the receiver.

Returns:

  • A copy of the receiver.



188
189
190
191
192
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 188

def clone
  self.class.new.tap do |clone|
    clone.aggregations = aggregations.map(&:clone)
  end
end

#composite(name, size: nil, &block) ⇒ Object

Adds a composite aggregation. For more information about the parameters:



136
137
138
139
140
141
142
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 136

def composite(name, size: nil, &block)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Composite.new(
      name, size: size, &block
    )
  )
end

#date_histogram(name, field:, calendar_interval:, format: nil) ⇒ Object

Adds a date_histogram type aggregation. For more information about the parameters



126
127
128
129
130
131
132
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 126

def date_histogram(name, field:, calendar_interval:, format: nil)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::DateHistogram.new(
      name, field: field, calendar_interval: calendar_interval, format: format
    )
  )
end

#filter(name, &block) ⇒ Object

Adds a filter type aggregation. For more information about the parameters



110
111
112
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 110

def filter(name, &block)
  add(::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Filter.new(name, &block))
end

#max(name, field:) ⇒ Object

Adds a max type aggregation. For information about the parameters



100
101
102
103
104
105
106
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 100

def max(name, field:)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Max.new(
      name, field: field
    )
  )
end

#merge(other) ⇒ self

Returns A new object, which represents the combination of the aggregations in the receiver and other.

Parameters:

  • The object to merge with the receiver.

Returns:

  • A new object, which represents the combination of the aggregations in the receiver and other.

Raises:

  • If other is not an instance of the same class (or a subclass of it).



178
179
180
181
182
183
184
185
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 178

def merge(other)
  klass = self.class
  raise TypeError, "Cannot merge #{klass} with #{other.class}" unless other.is_a?(klass)

  klass.new.tap do |merged|
    merged.aggregations = aggregations.map(&:clone) + other.aggregations.map(&:clone)
  end
end

#scripted_metric(name, map_script:, combine_script:, reduce_script:, init_script: nil) ⇒ Object

Adds an scripted_metric type aggregation. For information about the parameters



79
80
81
82
83
84
85
86
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 79

def scripted_metric(name, map_script:, combine_script:, reduce_script:, init_script: nil)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::ScriptedMetric.new(
      name, map_script: map_script, combine_script: combine_script,
            reduce_script: reduce_script, init_script: init_script
    )
  )
end

#sum(name, field:, missing: nil) ⇒ Object

Adds a sum type aggregation. For information about the parameters



53
54
55
56
57
58
59
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 53

def sum(name, field:, missing: nil)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Sum.new(
      name, field: field, missing: missing
    )
  )
end

#terms(name, **params) ⇒ Object

Adds a terms type aggregation. For information about the parameters



35
36
37
38
39
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 35

def terms(name, **params)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Terms.new(name, **params)
  )
end

#to_hHash

Returns a Hash with the correct format for the current list of aggregations. For example:

{
"aggs" => {
  "my-agg-name" => {
    "terms" => {
      "field" => "my_field"
    }
  },
  "my-average" => {
    "avg" => {
      "field" => "my_numeric_field"
    }
  }
}
}

Returns:

  • A Hash with the list of aggregations



163
164
165
166
167
168
169
170
171
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 163

def to_h
  return {} if none?

  {
    aggs: aggregations.inject({}) do |hash, aggregation|
      hash.merge(aggregation.to_h)
    end
  }
end

#top_hits(name, **params) ⇒ Object

Adds a top_hits type aggregation. For more information about the parameters



73
74
75
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 73

def top_hits(name, **params)
  add(::JayAPI::Elasticsearch::QueryBuilder::Aggregations::TopHits.new(name, **params))
end

#value_count(name, field:) ⇒ Object

Adds a value_count type aggregation. For information about the parameters



63
64
65
66
67
68
69
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 63

def value_count(name, field:)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::ValueCount.new(
      name, field: field
    )
  )
end