Class: Elastic::Nodes::Agg::Top

Inherits:
BaseAgg show all
Includes:
Concerns::Sortable
Defined in:
lib/elastic/nodes/agg/top.rb

Constant Summary

Constants included from Concerns::Sortable

Concerns::Sortable::MODES, Concerns::Sortable::ORDER

Instance Attribute Summary collapse

Attributes inherited from BaseAgg

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Sortable

#add_sort, #reset_sorts, #sorts

Methods inherited from BaseAgg

#initialize

Methods inherited from Base

#==, #traverse

Methods included from Support::Traversable

#pick_nodes, #traverse

Constructor Details

This class inherits a constructor from Elastic::Nodes::BaseAgg

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



11
12
13
# File 'lib/elastic/nodes/agg/top.rb', line 11

def field
  @field
end

Class Method Details

.build(_name, _field, _options = {}) ⇒ Object



5
6
7
8
9
# File 'lib/elastic/nodes/agg/top.rb', line 5

def self.build(_name, _field, _options = {})
  super(_name).tap do |node|
    node.field = _field
  end
end

Instance Method Details

#cloneObject



13
14
15
# File 'lib/elastic/nodes/agg/top.rb', line 13

def clone
  prepare_clone super
end

#handle_result(_raw, _formatter) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/elastic/nodes/agg/top.rb', line 28

def handle_result(_raw, _formatter)
  hit = _raw['hits']['hits'].first
  raw_value = hit ? hit['_source'][@field.to_s] : nil

  # TODO: apply formatter to value
  Elastic::Results::Metric.new raw_value
end

#render(_options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/elastic/nodes/agg/top.rb', line 21

def render(_options = {})
  top_hit_config = { '_source' => { 'includes' => [@field.to_s] }, 'size' => 1 }
  top_hit_config['sort'] = render_sorts if registered_sorts.count > 0

  { 'top_hits' => top_hit_config }
end

#simplifyObject



17
18
19
# File 'lib/elastic/nodes/agg/top.rb', line 17

def simplify
  prepare_clone super
end