Skip to content

Commit 788c6cb

Browse files
Jon Dalbergestolfo
Jon Dalberg
authored andcommitted
fix syntax and add spec
1 parent e98cf15 commit 788c6cb

File tree

2 files changed

+135
-37
lines changed

2 files changed

+135
-37
lines changed

lib/elasticsearch/dsl/search/aggregations/composite.rb

+36-37
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,42 @@
1616
# under the License.
1717

1818
module Elasticsearch
19-
module DSL
20-
module Search
21-
module Aggregations
22-
#
23-
# A multi-bucket aggregation that creates composite buckets from different sources.
24-
#
25-
# @example
26-
#
27-
# search do
28-
# aggregation :things do
29-
# composite do
30-
# size 2000
31-
# sources [
32-
# { thing1: { terms: { field: 'thing1.field1' } } },
33-
# { thing2: { terms: { field: 'thing2.field2' } } }
34-
# ]
35-
# after after_key
36-
# end
37-
# end
38-
# end
39-
#
40-
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
41-
#
42-
class Composite
43-
include BaseAggregationComponent
44-
45-
option_method :size
46-
option_method :sources
47-
option_method :after
48-
49-
def to_hash(_options={})
50-
super
51-
# remove :after if no value is given
52-
@hash[self.name.to_sym].delete(:after) if @hash[self.name.to_sym][:after].nil?
53-
54-
@hash
55-
end
19+
module DSL
20+
module Search
21+
module Aggregations
22+
#
23+
# A multi-bucket aggregation that creates composite buckets from different sources.
24+
#
25+
# @example
26+
#
27+
# search do
28+
# aggregation :things do
29+
# composite do
30+
# size 2000
31+
# sources [
32+
# { thing1: { terms: { field: 'thing1.field1' } } },
33+
# { thing2: { terms: { field: 'thing2.field2' } } }
34+
# ]
35+
# after after_key
36+
# end
37+
# end
38+
# end
39+
#
40+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
41+
#
42+
class Composite
43+
include BaseAggregationComponent
44+
45+
option_method :size
46+
option_method :sources
47+
option_method :after
48+
49+
def to_hash(_options={})
50+
super
51+
# remove :after if no value is given
52+
@hash[name.to_sym].delete(:after) if @hash[name.to_sym].is_a?(Hash) && @hash[name.to_sym][:after].nil?
53+
54+
@hash
5655
end
5756
end
5857
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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::Composite 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(composite: {})
30+
end
31+
end
32+
33+
context 'when options methods are called' do
34+
35+
let(:search) do
36+
described_class.new(:foo)
37+
end
38+
39+
describe '#size' do
40+
41+
before do
42+
search.size(2_000)
43+
end
44+
45+
it 'applies the option' do
46+
expect(search.to_hash[:composite][:foo][:size]).to eq(2_000)
47+
end
48+
end
49+
50+
describe '#sources' do
51+
52+
before do
53+
search.sources('bar')
54+
end
55+
56+
it 'applies the option' do
57+
expect(search.to_hash[:composite][:foo][:sources]).to eq('bar')
58+
end
59+
end
60+
61+
describe '#after' do
62+
context 'when after is not given' do
63+
before do
64+
search.size(2_000)
65+
end
66+
67+
it 'applies the option' do
68+
expect(search.to_hash[:composite][:foo].keys).not_to include(:after)
69+
end
70+
end
71+
72+
context 'when after is given' do
73+
before do
74+
search.after('fake_after_key')
75+
end
76+
77+
it 'applies the option' do
78+
expect(search.to_hash[:composite][:foo][:after]).to eq('fake_after_key')
79+
end
80+
end
81+
end
82+
end
83+
84+
describe '#initialize' do
85+
86+
context 'when a block is provided' do
87+
88+
let(:search) do
89+
described_class.new(:foo) do
90+
size 2_000
91+
end
92+
end
93+
94+
it 'executes the block' do
95+
expect(search.to_hash).to eq({ composite: { foo: { size: 2_000 } } })
96+
end
97+
end
98+
end
99+
end

0 commit comments

Comments
 (0)