Class: ElasticSearch::AggregationRelation

Inherits:
Object
  • Object
show all
Includes:
AggregatableRelation, FilterableRelation
Defined in:
lib/elastic_search/aggregation_relation.rb

Overview

The ElasticSearch::AggregationRelation class puts together everything required to use the ElasticSearch aggregation framework via mixins and adds a method to convert it to a hash format to be used in the request.

Instance Method Summary collapse

Methods included from AggregatableRelation

#aggregate, included

Methods included from FilterableRelation

#exists, #exists_not, #filter, included, #match_all, #must, #must_not, #range, #search, #should, #where, #where_not

Instance Method Details

#freshElasticSearch::AggregationRelation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Simply dups the object for api compatability.

Returns:



52
53
54
# File 'lib/elastic_search/aggregation_relation.rb', line 52

def fresh
  dup
end

#to_hashHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the aggregation to a hash format that can be used in the request.

Returns:

  • (Hash)

    A hash version of the aggregation



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
# File 'lib/elastic_search/aggregation_relation.rb', line 17

def to_hash
  res = {}
  res[:aggregations] = aggregation_values if aggregation_values

  if must_values || search_values || must_not_values || should_values || filter_values
    if ElasticSearch.version.to_i >= 2
      res[:filter] = {
        bool: {}.
          merge(must_values || search_values ? { must: (must_values || []) + (search_values || []) } : {}).
          merge(must_not_values ? { must_not: must_not_values } : {}).
          merge(should_values ? { should: should_values } : {}).
          merge(filter_values ? { filter: filter_values } : {})
      }
    else
      filters = (filter_values || []) + (must_not_values || []).map { |must_not_value| { not: must_not_value } }

      queries = {}.
        merge(must_values || search_values ? { must: (must_values || []) + (search_values || []) } : {}).
        merge(should_values ? { should: should_values } : {})

      filters_and_queries = filters + (queries.size > 0 ? [bool: queries] : [])

      res[:filter] = filters_and_queries.size > 1 ? { and: filters_and_queries } : filters_and_queries.first
    end
  end

  res
end