Class: Skala::ElasticsearchAdapter::Search::RequestTransformation::AddAggregations

Inherits:
Transformator::Transformation::Step
  • Object
show all
Defined in:
lib/skala/elasticsearch_adapter/search/request_transformation/add_aggregations.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
44
45
46
47
48
49
# File 'lib/skala/elasticsearch_adapter/search/request_transformation/add_aggregations.rb', line 7

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