class EagleSearch::Interpreter::Aggregation

Public Class Methods

new(aggregations) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 3
def initialize(aggregations)
  @aggregations = aggregations
end

Public Instance Methods

payload() click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 7
def payload
  @payload ||= build_aggregation(@aggregations)
end

Private Instance Methods

build_aggregation(aggregation) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 12
def build_aggregation(aggregation)
  payload = {}

  if terms_aggregation?(aggregation)
    return { aggregation => build_terms_aggregation(aggregation) }
  elsif aggregation.is_a?(Array)
    aggregation.each do |agg|
      payload.merge!(build_aggregation(agg))
    end
  elsif aggregation.is_a?(Hash)
    aggregation.each do |field_name, aggregation_body|
      if stats_aggregation?(aggregation_body)
        payload.merge!({ field_name => build_stats_aggregation(field_name) })
      elsif range_aggregation?(aggregation_body)
        payload.merge!({ field_name => build_range_aggregation(field_name, aggregation_body) })
      else
        payload.merge!(build_aggregation(field_name))
        payload[field_name].merge!(aggregations: build_aggregation(aggregation_body))
      end
    end
  end

  payload
end
build_range_aggregation(field, range_aggregation) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 53
def build_range_aggregation(field, range_aggregation)
  agg = {
    range: {
      field: field,
      ranges: []
    }
  }

  range_aggregation[:ranges].each do |range|
    if range.is_a?(Range)
      agg[:range][:ranges] << {
        from: range.min,
        to: range.max
      }
    else
      agg[:range][:ranges] << range
    end
  end

  agg
end
build_stats_aggregation(field) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 45
def build_stats_aggregation(field)
  {
    stats: {
      field: field
    }
  }
end
build_terms_aggregation(field) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 37
def build_terms_aggregation(field)
  {
    terms: {
      field: field
    }
  }
end
range_aggregation?(aggregation) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 83
def range_aggregation?(aggregation)
  aggregation.is_a?(Hash) && aggregation[:ranges]
end
stats_aggregation?(aggregation) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 75
def stats_aggregation?(aggregation)
  aggregation.is_a?(Hash) && aggregation[:type] == "stats"
end
terms_aggregation?(aggregation) click to toggle source
# File lib/eagle_search/interpreter/aggregation.rb, line 79
def terms_aggregation?(aggregation)
  aggregation.is_a?(Symbol) || aggregation.is_a?(String)
end