Class: Skala::ElasticsearchAdapter::Search::ResultTransformation::SetFacets

Inherits:
Transformator::Transformation::Step
  • Object
show all
Defined in:
lib/skala/elasticsearch_adapter/search/result_transformation/set_facets.rb

Instance Method Summary collapse

Instance Method Details

#callObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/skala/elasticsearch_adapter/search/result_transformation/set_facets.rb', line 7

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