Sha256: ae205490078966e2334702114be2eb3e8f9251f677ed6993c31c6f681b6d6d40

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require "transformator/transformation/step"
require_relative "../request_transformation"

class Skala::ElasticsearchAdapter::Search::RequestTransformation::
  AddAggregations < Transformator::Transformation::Step

  def call
    source.facets.try(:each) do |_facet|
      target["aggregations"] ||= {}

      elasticsearch_aggregation =
      if _facet.is_a?(Skala::Adapter::Search::Request::HistogramFacet)
        {
          "histogram" => {
            "field" => _facet.field,
            "interval" => _facet.interval
          }
          .compact
        }
      elsif _facet.is_a?(Skala::Adapter::Search::Request::RangeFacet)
        {
          "range" => {
            "field" => _facet.field,
            "ranges" => _facet.ranges.map do |_range|
              {
                "key" => _range.key,
                "from" => _range.from,
                "to" => _range.to
              }
              .compact
            end
          }  
        }
      elsif _facet.is_a?(Skala::Adapter::Search::Request::TermsFacet)
        {
          "terms" => {
            "field" => _facet.field,
            "size" => _facet.size,
            "shard_size" => (_facet.size * 3 if _facet.size)
          }
          .compact
        }
      end
      
      if elasticsearch_aggregation
        target["aggregations"][_facet.name] = elasticsearch_aggregation
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skala-1.1.0 lib/skala/elasticsearch_adapter/search/request_transformation/add_aggregations.rb