Class: ElasticsearchDslBuilder::DSL::Search::Aggregations::Range

Inherits:
Aggregation
  • Object
show all
Defined in:
lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb

Instance Method Summary collapse

Methods inherited from Aggregation

#aggregation

Constructor Details

#initializeRange

Returns a new instance of Range.



6
7
8
9
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb', line 6

def initialize
  @type = :range
  super()
end

Instance Method Details

#field(field) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb', line 11

def field(field)
  raise ArgumentError, 'field must be a String' unless field.instance_of?(String)
  @field = field
  self
end

#keyed(keyed) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb', line 23

def keyed(keyed)
  raise ArgumentError, 'keyed must be a boolean' unless !!keyed == keyed
  @keyed = keyed
  self
end

#range(from: nil, to: nil, key: nil) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb', line 29

def range(from: nil, to: nil, key: nil)
  @ranges ||= []
  raise ArgumentError, 'must pass at least one of from or to' if from.nil? && to.nil?
  from_valid = from.instance_of?(String) || from.is_a?(Numeric)
  raise ArgumentError, 'from must be String or Numeric' if !from.nil? && !from_valid
  to_valid = to.instance_of?(String) || to.is_a?(Numeric)
  raise ArgumentError, 'to must be String or Numeric' if !to.nil? && !to_valid
  range = {}
  range.update(from: from) if from
  range.update(to: to) if to
  range.update(key: key) if key
  @ranges << range
  self
end

#script(script) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb', line 17

def script(script)
  raise ArgumentError, 'script must be a Script' unless script.instance_of?(Script)
  @script = script
  self
end

#to_hashObject

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
# File 'lib/elasticsearch_dsl_builder/dsl/search/aggregations/range.rb', line 44

def to_hash
  raise ArgumentError, 'must have set at least one of [field, script]' if @field.nil? && @script.nil?
  @aggregation = {}
  @aggregation.update(field: @field) if @field
  @aggregation.update(script: @script.to_hash) if @script
  @aggregation.update(keyed: @keyed) if @keyed
  @aggregation.update(ranges: @ranges) if @ranges
  super
end