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



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

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



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

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



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

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

#cloneself

Returns A copy of the receiver.

Returns:

  • (self)

    A copy of the receiver.



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

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:



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

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



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

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



112
113
114
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 112

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



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

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:

  • other (self)

    The object to merge with the receiver.

Returns:

  • (self)

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

Raises:

  • (TypeError)

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



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

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



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

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



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

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

#terms(name, field: nil, script: nil, size: nil, order: nil) ⇒ Object

Adds a terms type aggregation. For information about the parameters



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

def terms(name, field: nil, script: nil, size: nil, order: nil)
  add(
    ::JayAPI::Elasticsearch::QueryBuilder::Aggregations::Terms.new(
      name, field: field, script: script, size: size, order: order
    )
  )
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:

  • (Hash)

    A Hash with the list of aggregations



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

def to_h
  return {} if none?

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

#top_hits(name, size:) ⇒ Object

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



75
76
77
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 75

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

#value_count(name, field:) ⇒ Object

Adds a value_count type aggregation. For information about the parameters



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

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