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