Skip to content

Commit ea89f20

Browse files
committed
[DSL] Implement support for bucket_sort pipeline aggregation
1 parent d6f9365 commit ea89f20

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
module Elasticsearch
19+
module DSL
20+
module Search
21+
module Aggregations
22+
23+
# A parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation.
24+
#
25+
# @example Passing the options as a Hash
26+
#
27+
# aggregation :sales_bucket_filter do
28+
# bucket_sort gap_policy: 'insert_zero'
29+
# end
30+
#
31+
# @example Passing the options as a block
32+
#
33+
# aggregation :sales_bucket_sort do
34+
# bucket_sort do
35+
# sort do
36+
# by :total_sales, order: 'desc'
37+
# end
38+
# size 3
39+
# end
40+
# end
41+
#
42+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html
43+
#
44+
class BucketSort
45+
include BaseAggregationComponent
46+
47+
def sort(*args, &block)
48+
if !args.empty? || block
49+
@sort = Sort.new(*args, &block)
50+
self
51+
else
52+
@sort
53+
end
54+
end
55+
56+
def to_hash
57+
call
58+
59+
if @aggregations
60+
@hash[:aggregations] = @aggregations.to_hash
61+
end
62+
63+
@hash[name].merge!(sort: @sort.to_hash) if @sort
64+
@hash
65+
end
66+
67+
option_method :from
68+
option_method :size
69+
option_method :gap_policy
70+
end
71+
end
72+
end
73+
end
74+
end
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
require 'spec_helper'
19+
20+
describe Elasticsearch::DSL::Search::Aggregations::BucketSort do
21+
22+
let(:search) do
23+
described_class.new
24+
end
25+
26+
describe '#to_hash' do
27+
28+
it 'can be converted to a hash' do
29+
expect(search.to_hash).to eq(bucket_sort: {})
30+
end
31+
end
32+
33+
context 'when options methods are called' do
34+
35+
let(:search) do
36+
described_class.new
37+
end
38+
39+
describe '#sort' do
40+
41+
before do
42+
search.sort do
43+
by :category, order: 'desc'
44+
end
45+
end
46+
47+
it 'applies the option' do
48+
expect(search.to_hash[:bucket_sort][:sort]).to eq([{ category: { order: 'desc' } }])
49+
end
50+
end
51+
52+
describe '#from' do
53+
54+
before do
55+
search.from(5)
56+
end
57+
58+
it 'applies the option' do
59+
expect(search.to_hash[:bucket_sort][:from]).to eq(5)
60+
end
61+
end
62+
63+
describe '#size' do
64+
65+
before do
66+
search.size(10)
67+
end
68+
69+
it 'applies the option' do
70+
expect(search.to_hash[:bucket_sort][:size]).to eq(10)
71+
end
72+
end
73+
74+
describe '#gap_policy' do
75+
76+
before do
77+
search.gap_policy('insert_zero')
78+
end
79+
80+
it 'applies the option' do
81+
expect(search.to_hash[:bucket_sort][:gap_policy]).to eq('insert_zero')
82+
end
83+
end
84+
end
85+
86+
describe '#initialize' do
87+
88+
context 'when a block is provided' do
89+
90+
let(:search) do
91+
described_class.new do
92+
sort do
93+
by :total_sales, order: 'desc'
94+
end
95+
size 3
96+
end
97+
end
98+
99+
it 'executes the block' do
100+
expect(search.to_hash).to eq(bucket_sort: { size: 3, sort: [{ total_sales: { order: 'desc' } }] })
101+
end
102+
end
103+
end
104+
end

0 commit comments

Comments
 (0)