File: search_aggregations_test.rb

package info (click to toggle)
ruby-elasticsearch 7.17.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,820 kB
  • sloc: ruby: 44,308; sh: 16; makefile: 2
file content (278 lines) | stat: -rw-r--r-- 10,627 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

require 'test_helper'

module Elasticsearch
  module Test
    class AggregationsIntegrationTest < ::Elasticsearch::Test::IntegrationTestCase
      include Elasticsearch::DSL::Search

      context "Aggregations integration" do
        setup do
          @client.indices.create index: 'test', body: {
              mappings: {
                  properties: {
                      tags: {type: 'keyword'}
                  }
              }
          }
          @client.index index: 'test', id: '1', body: { title: 'A', tags: %w[one], clicks: 5 }
          @client.index index: 'test', id: '2', body: { title: 'B', tags: %w[one two], clicks: 15 }
          @client.index index: 'test', id: '3', body: { title: 'C', tags: %w[one three], clicks: 20 }
          @client.indices.refresh index: 'test'
        end

        context "with a terms aggregation" do
          should "return tag counts" do
            response = @client.search index: 'test', body: search {
              aggregation :tags do
                terms field: 'tags'
              end
            }.to_hash

            assert_equal 3, response['aggregations']['tags']['buckets'].size
            assert_equal 'one', response['aggregations']['tags']['buckets'][0]['key']
          end

          should "return approximate tag counts" do
            response = @client.search index: 'test', body: search {
              aggregation :tags do
                cardinality field: 'tags'
              end
            }.to_hash

            assert_equal 3, response['aggregations']['tags']['value']
          end

          should "return tag counts per clicks range as an inner (nested) aggregation" do
            response = @client.search index: 'test', body: search {
              aggregation :clicks do
                range field: 'clicks' do
                  key :low, to: 10
                  key :mid, from: 10, to: 20

                  aggregation :tags do
                    terms field: 'tags'
                  end
                end
              end
            }.to_hash

            assert_equal 2, response['aggregations']['clicks']['buckets'].size
            assert_equal 1, response['aggregations']['clicks']['buckets']['low']['doc_count']
            assert_equal 'one', response['aggregations']['clicks']['buckets']['low']['tags']['buckets'][0]['key']
          end

          should "define multiple aggregations" do
            response = @client.search index: 'test', body: search {
              aggregation :clicks do
                range field: 'clicks' do
                  key :low, to: 10
                  key :mid, from: 10, to: 20

                  aggregation :tags do
                    terms { field 'tags' }
                  end
                end
              end

              aggregation :min_clicks do
                min field: 'clicks'
              end

              aggregation :max_clicks do
                max field: 'clicks'
              end

              aggregation :sum_clicks do
                sum field: 'clicks'
              end

              aggregation :avg_clicks do
                avg field: 'clicks'
              end
            }.to_hash

            assert_equal 2, response['aggregations']['clicks']['buckets'].size
            assert_equal 1, response['aggregations']['clicks']['buckets']['low']['doc_count']
            assert_equal 'one', response['aggregations']['clicks']['buckets']['low']['tags']['buckets'][0]['key']

            assert_equal 5,  response['aggregations']['min_clicks']['value']
            assert_equal 20, response['aggregations']['max_clicks']['value']
            assert_equal 40, response['aggregations']['sum_clicks']['value']
            assert_equal 13, response['aggregations']['avg_clicks']['value'].to_i
          end

          should "define a global aggregation" do
            response = @client.search index: 'test', body: search {
                query do
                  bool filter: { terms: { tags: ['two'] } }
                end

                aggregation :avg_clicks do
                  avg field: 'clicks'
                end

                aggregation :all_documents do
                  global do
                    aggregation :avg_clicks do
                      avg field: 'clicks'
                    end
                  end
                end
            }.to_hash

            assert_equal 15, response['aggregations']['avg_clicks']['value'].to_i
            assert_equal 13, response['aggregations']['all_documents']['avg_clicks']['value'].to_i
          end

          should "return statistics on clicks" do
            response = @client.search index: 'test', body: search {
              aggregation :stats_clicks do
                stats field: 'clicks'
              end
              aggregation :value_count do
                value_count field: 'clicks'
              end
            }.to_hash

            assert_equal 3,  response['aggregations']['stats_clicks']['count']
            assert_equal 5,  response['aggregations']['stats_clicks']['min']
            assert_equal 20, response['aggregations']['stats_clicks']['max']
            assert_equal 40, response['aggregations']['stats_clicks']['sum']
            assert_equal 13, response['aggregations']['stats_clicks']['avg'].to_i
            assert_equal 3,  response['aggregations']['value_count']['value']
          end

          should "return percentiles on clicks" do
            response = @client.search index: 'test', body: search {
              aggregation :percentiles do
                percentiles field: 'clicks'
              end
            }.to_hash

            assert_equal 20, response['aggregations']['percentiles']['values']['99.0'].round
          end

          should "return percentile ranks on clicks" do
            response = @client.search index: 'test', body: search {
              aggregation :percentiles do
                percentile_ranks field: 'clicks', values: [5]
              end
            }.to_hash

            assert_equal 17, response['aggregations']['percentiles']['values']['5.0'].round
          end

          should "return top hits per tag" do
            response = @client.search index: 'test', body: search {
              aggregation :tags do
                terms do
                  field 'tags'
                  size  5

                  aggregation :top_hits do
                    top_hits sort: [ clicks: { order: 'desc' } ], _source: { include: 'title' }
                  end
                end
              end
            }.to_hash

            assert_equal 3, response['aggregations']['tags']['buckets'][0]['top_hits']['hits']['hits'].size
            assert_equal 'C', response['aggregations']['tags']['buckets'][0]['top_hits']['hits']['hits'][0]['_source']['title']
          end

          should "calculate clicks for a tag" do
            response = @client.search index: 'test', body: search {
              aggregation :clicks_for_one do
                scripted_metric do
                  init_script "state.transactions = []"
                  map_script  "if (doc['tags'].value.contains('one')) { state.transactions.add(doc['clicks'].value) }"
                  combine_script "double sum = 0; for (t in state.transactions) { sum += t } return sum"
                  reduce_script "double sum = 0; for (a in states) { sum += a } return sum"
                end
              end
            }.to_hash

            assert_equal 40, response['aggregations']['clicks_for_one']['value']
          end

          should "limit the scope with a filter" do
            response = @client.search index: 'test', body: search {
              aggregation :clicks_for_one do
                filter terms: { tags: ['one'] } do
                  aggregation :sum_clicks do
                    sum field: 'clicks'
                  end
                end
              end
            }.to_hash

            assert_equal 40, response['aggregations']['clicks_for_one']['sum_clicks']['value']
          end
        end

        should "return aggregations for multiple filters" do
          response = @client.search index: 'test', body: search {
            aggregation :avg_clicks_per_tag do
              filters do
                filters one: { terms: { tags: ['one'] } },
                        two: { terms: { tags: ['two'] } }
                aggregation :avg do
                  avg field: 'clicks'
                end
              end
            end
          }.to_hash

          assert_equal 13, response['aggregations']['avg_clicks_per_tag']['buckets']['one']['avg']['value'].to_i
          assert_equal 15, response['aggregations']['avg_clicks_per_tag']['buckets']['two']['avg']['value'].to_i
        end

        should "return a histogram on clicks" do
          response = @client.search index: 'test', body: search {
            aggregation :clicks_histogram do
              histogram do
                field   'clicks'
                interval 10
              end
            end
          }.to_hash

          assert_equal 3,  response['aggregations']['clicks_histogram']['buckets'].size
          assert_equal 10, response['aggregations']['clicks_histogram']['buckets'][1]['key']
          assert_equal 1,  response['aggregations']['clicks_histogram']['buckets'][1]['doc_count']
        end

        should "return a histogram with empty buckets on clicks" do
          response = @client.search index: 'test', body: search {
            aggregation :clicks_histogram do
              histogram do
                field   'clicks'
                interval 2
                min_doc_count 0
              end
            end
          }.to_hash

          assert_equal 9, response['aggregations']['clicks_histogram']['buckets'].size
        end
      end
    end
  end
end