Class: ElasticsearchDslBuilder::DSL::Search::Aggregations::DateRange

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

Instance Method Summary collapse

Methods inherited from Aggregation

#aggregation

Constructor Details

#initialize(field = nil) ⇒ DateRange

Returns a new instance of DateRange.



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

def initialize(field = nil)
  @type = :date_range
  field(field)
  super()
end

Instance Method Details

#field(field) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#format(format) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#keyed(keyed) ⇒ Object

Raises:

  • (ArgumentError)


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

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)


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

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

#to_hashObject



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

def to_hash
  @aggregation = { field: @field }
  @aggregation.update(format: @format) if @format
  @aggregation.update(keyed: @keyed) if @keyed
  @aggregation.update(ranges: @ranges) if @ranges
  super
end