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

Inherits:
Aggregation
  • Object
show all
Defined in:
lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb

Overview

Represents a terms aggregation in Elasticsearch. Information about this type of aggregation can be found in: www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html

Instance Attribute Summary collapse

Attributes inherited from Aggregation

#name

Instance Method Summary collapse

Methods inherited from Aggregation

#aggs

Constructor Details

#initialize(name, field: nil, script: nil, size: nil, order: nil) ⇒ Terms

Returns a new instance of Terms.

Parameters:

  • name (String)

    The name used by Elasticsearch to identify each of the aggregations.

  • field (String) (defaults to: nil)

    The field whose unique values should be counted.

  • script (JayAPI::Elasticsearch::QueryBuilder::Script) (defaults to: nil)

    If a script is given the aggregation will count the unique values returned by the script instead of the unique values in a specific field.

  • size (Integer) (defaults to: nil)

    By default the aggregation returns the top 10 unique values (the ones with the higher frequency). By specifying a size this can be changed.

  • order (Hash) (defaults to: nil)

    A custom order for the buckets produced by the aggregation. By default, the terms aggregation orders terms by descending document _count. This can be changed by providing a custom order hash.

Raises:

  • (ArgumentError)

    If neither a field nor a script are given or if both of them are given. Only one should be present.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 35

def initialize(name, field: nil, script: nil, size: nil, order: nil)
  if (field.present? && script.present?) || (field.blank? && script.blank?)
    raise ArgumentError, "Either 'field' or 'script' must be provided"
  end

  super(name)

  @field = field
  @script = script
  @size = size
  @order = order
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



17
18
19
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 17

def field
  @field
end

#orderObject (readonly)

Returns the value of attribute order.



17
18
19
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 17

def order
  @order
end

#scriptObject (readonly)

Returns the value of attribute script.



17
18
19
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 17

def script
  @script
end

#sizeObject (readonly)

Returns the value of attribute size.



17
18
19
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 17

def size
  @size
end

Instance Method Details

#cloneself

Returns A copy of the receiver.

Returns:

  • (self)

    A copy of the receiver.



49
50
51
52
53
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 49

def clone
  self.class.new(name, field: field, script: script, size: size, order: order&.deep_dup).tap do |copy|
    copy.aggregations = aggregations.clone
  end
end

#to_hHash

Returns The Hash representation of the Aggregation. Properly formatted for Elasticsearch.

Returns:

  • (Hash)

    The Hash representation of the Aggregation. Properly formatted for Elasticsearch.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jay_api/elasticsearch/query_builder/aggregations/terms.rb', line 57

def to_h
  super do
    {
      terms: {
        field: field,
        size: size,
        script: script&.to_h,
        order: order
      }.compact
    }
  end
end