Module: SearchFlip::Aggregatable
- Included in:
- Aggregation, Criteria
- Defined in:
- lib/search_flip/aggregatable.rb
Overview
The SearchFlip::Aggregatable mixin provides handy methods for using the Elasticsearch aggregation framework, which can be chained with each other, all other criteria methods and even nested.
Class Method Summary collapse
Instance Method Summary collapse
-
#aggregate(field_or_hash, options = {}, &block) ⇒ Object
Adds an arbitrary aggregation to the request which can be chained as well as nested.
Class Method Details
.included(base) ⇒ Object
11 12 13 14 15 |
# File 'lib/search_flip/aggregatable.rb', line 11 def self.included(base) base.class_eval do attr_accessor :aggregation_values end end |
Instance Method Details
#aggregate(field_or_hash, options = {}, &block) ⇒ Object
Adds an arbitrary aggregation to the request which can be chained as well as nested. Check out the examples and Elasticsearch docs for further details.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/search_flip/aggregatable.rb', line 53 def aggregate(field_or_hash, = {}, &block) fresh.tap do |criteria| hash = field_or_hash.is_a?(Hash) ? field_or_hash : { field_or_hash => { terms: { field: field_or_hash }.merge() } } if block aggregation = yield(SearchFlip::Aggregation.new(target: target)) if field_or_hash.is_a?(Hash) aggregation_hash = field_or_hash.values.first aggregation_hash = aggregation_hash[:top_hits] if aggregation_hash.is_a?(Hash) && aggregation_hash.key?(:top_hits) aggregation_hash = aggregation_hash["top_hits"] if aggregation_hash.is_a?(Hash) && aggregation_hash.key?("top_hits") aggregation_hash.merge!(aggregation.to_hash) else hash[field_or_hash].merge!(aggregation.to_hash) end end criteria.aggregation_values = (aggregation_values || {}).merge(hash) end end |