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/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, Cardinality, Composite, DateHistogram, Filter, Max, ScriptedMetric, Sum, Terms, TopHits, ValueCount

Instance Method Summary collapse

Constructor Details

#initializeAggregations

Returns a new instance of Aggregations.



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

def initialize
  @aggregations = []
end

Instance Method Details

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

Adds an avg type aggregation. For information about the parameters



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

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

#cardinality(name, field:) ⇒ Object

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



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

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.



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

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:



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

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



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

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



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

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



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

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).



169
170
171
172
173
174
175
176
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 169

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



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

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



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

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



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

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



154
155
156
157
158
159
160
161
162
# File 'lib/jay_api/elasticsearch/query_builder/aggregations.rb', line 154

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



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

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



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

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