Sha256: 989f44572171d1e182beb838a305d94873cbf9aaf55fbdfed90053b7f76b766b

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

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

class Skala::ElasticsearchAdapter::Search::ResultTransformation::
  SetFacets < Transformator::Transformation::Step

  def call
    if result_aggregations = source["aggregations"]
      requested_facets = transformation.search_request.facets
      result_facets = []

      result_aggregations.each do |_key, _value|
        if requested_facet = requested_facets.find { |_facet| _facet.name == _key }

          result_facets << if requested_facet.is_a?(Skala::Adapter::Search::Request::HistogramFacet)
            Skala::Adapter::Search::Result::HistogramFacet.new({
              field: requested_facet.field,
              name: _key,
              entries: _value["buckets"].map do |_bucket|
                {
                  count: _bucket["doc_count"],
                  key: _bucket["key"]
                }
              end
            })
          elsif requested_facet.is_a?(Skala::Adapter::Search::Request::TermsFacet)
            Skala::Adapter::Search::Result::TermsFacet.new({
              field: requested_facet.field,
              name: _key,
              terms: _value["buckets"].map do |_bucket|
                {
                  term: _bucket["key"],
                  count: _bucket["doc_count"]
                }
              end
            })
          end
        end
      end

      target.facets = result_facets if result_facets.present?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skala-1.1.0 lib/skala/elasticsearch_adapter/search/result_transformation/set_facets.rb