Class: SearchFlip::Aggregation

Inherits:
Object
  • Object
show all
Includes:
Aggregatable, Customable, Explainable, Filterable, Highlightable, Paginatable, Sortable, Sourceable
Defined in:
lib/search_flip/aggregation.rb

Overview

The SearchFlip::Aggregation 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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from Customable

#custom, included

Methods included from Sortable

included, #resort, #sort

Methods included from Sourceable

included, #source

Methods included from Explainable

#explain, included

Methods included from Highlightable

#highlight, included

Methods included from Paginatable

included, #limit, #limit_value_with_default, #offset, #offset_value_with_default, #page, #paginate, #per

Methods included from Aggregatable

#aggregate, included

Methods included from Filterable

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

Constructor Details

#initialize(target:) ⇒ Aggregation

Returns a new instance of Aggregation.



18
19
20
# File 'lib/search_flip/aggregation.rb', line 18

def initialize(target:)
  @target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/search_flip/aggregation.rb', line 107

def method_missing(name, *args, &block)
  if target.respond_to?(name)
    merge(target.send(name, *args, &block))
  else
    super
  end
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



16
17
18
# File 'lib/search_flip/aggregation.rb', line 16

def target
  @target
end

Instance Method Details

#freshSearchFlip::Aggregation

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:



123
124
125
# File 'lib/search_flip/aggregation.rb', line 123

def fresh
  dup
end

#merge(other) ⇒ SearchFlip::Aggregation

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.

Merges a criteria into the aggregation.

Parameters:

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/search_flip/aggregation.rb', line 69

def merge(other)
  other = other.criteria

  fresh.tap do |aggregation|
    unsupported_methods = [
      :profile_value, :failsafe_value, :terminate_after_value, :timeout_value, :scroll_args,
      :suggest_values, :includes_values, :preload_values, :eager_load_values, :post_must_values,
      :post_must_not_values, :post_filter_values, :preference_value, :search_type_value,
      :routing_value
    ]

    unsupported_methods.each do |unsupported_method|
      unless other.send(unsupported_method).nil?
        raise(SearchFlip::NotSupportedError, "Using #{unsupported_method} within aggregations is not supported")
      end
    end

    aggregation.source_value = other.source_value if other.source_value
    aggregation.offset_value = other.offset_value if other.offset_value
    aggregation.limit_value = other.limit_value if other.limit_value
    aggregation.scroll_args = other.scroll_args if other.scroll_args
    aggregation.explain_value = other.explain_value unless other.explain_value.nil?

    aggregation.sort_values = (aggregation.sort_values || []) + other.sort_values if other.sort_values
    aggregation.must_values = (aggregation.must_values || []) + other.must_values if other.must_values
    aggregation.must_not_values = (aggregation.must_not_values || []) + other.must_not_values if other.must_not_values
    aggregation.filter_values = (aggregation.filter_values || []) + other.filter_values if other.filter_values

    aggregation.highlight_values = (aggregation.highlight_values || {}).merge(other.highlight_values) if other.highlight_values
    aggregation.custom_value = (aggregation.custom_value || {}).merge(other.custom_value) if other.custom_value
    aggregation.aggregation_values = (aggregation.aggregation_values || {}).merge(other.aggregation_values) if other.aggregation_values
  end
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/search_flip/aggregation.rb', line 103

def respond_to_missing?(name, *args)
  target.respond_to?(name, *args) || super
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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/search_flip/aggregation.rb', line 28

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

  if must_values || must_not_values || filter_values
    if target.connection.distribution || target.connection.version.to_i >= 2
      res[:filter] = {
        bool: {}
          .merge(must_values ? { must: must_values } : {})
          .merge(must_not_values ? { must_not: must_not_values } : {})
          .merge(filter_values ? { filter: filter_values } : {})
      }
    else
      filters = (filter_values || []) + (must_not_values || []).map { |must_not_value| { not: must_not_value } }
      queries = must_values ? { must: must_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.update(from: offset_value_with_default, size: limit_value_with_default) if offset_value || limit_value

  res[:explain] = explain_value unless explain_value.nil?
  res[:highlight] = highlight_values if highlight_values
  res[:sort] = sort_values if sort_values
  res[:_source] = source_value unless source_value.nil?

  res.update(custom_value) if custom_value

  res
end