Skip to content

Commit c57990d

Browse files
defgenxestolfo
authored andcommitted
[DSL] Allow Bool query and Bool filter methods to take objects as arguments
1 parent a6f208b commit c57990d

File tree

4 files changed

+114
-6
lines changed

4 files changed

+114
-6
lines changed

lib/elasticsearch/dsl/search/filters/bool.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,21 @@ class Bool
5959

6060
def must(*args, &block)
6161
@hash[name][:must] ||= []
62-
value = Filter.new(*args, &block).to_hash
62+
value = args.empty? ? Filter.new(*args, &block).to_hash : args.first.to_hash
6363
@hash[name][:must].push(value).flatten! unless @hash[name][:must].include?(value)
6464
self
6565
end
6666

6767
def must_not(*args, &block)
6868
@hash[name][:must_not] ||= []
69-
value = Filter.new(*args, &block).to_hash
69+
value = args.empty? ? Filter.new(*args, &block).to_hash : args.first.to_hash
7070
@hash[name][:must_not].push(value).flatten! unless @hash[name][:must_not].include?(value)
7171
self
7272
end
7373

7474
def should(*args, &block)
7575
@hash[name][:should] ||= []
76-
value = Filter.new(*args, &block).to_hash
76+
value = args.empty? ? Filter.new(*args, &block).to_hash : args.first.to_hash
7777
@hash[name][:should].push(value).flatten! unless @hash[name][:should].include?(value)
7878
self
7979
end

lib/elasticsearch/dsl/search/queries/bool.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ class Bool
5858

5959
def must(*args, &block)
6060
@hash[name][:must] ||= []
61-
value = Query.new(*args, &block).to_hash
61+
value = args.empty? ? Query.new(*args, &block).to_hash : args.first.to_hash
6262
@hash[name][:must].push(value).flatten! unless @hash[name][:must].include?(value)
6363
self
6464
end
6565

6666
def must_not(*args, &block)
6767
@hash[name][:must_not] ||= []
68-
value = Query.new(*args, &block).to_hash
68+
value = args.empty? ? Query.new(*args, &block).to_hash : args.first.to_hash
6969
@hash[name][:must_not].push(value).flatten! unless @hash[name][:must_not].include?(value)
7070
self
7171
end
7272

7373
def should(*args, &block)
7474
@hash[name][:should] ||= []
75-
value = Query.new(*args, &block).to_hash
75+
value = args.empty? ? Query.new(*args, &block).to_hash : args.first.to_hash
7676
@hash[name][:should].push(value).flatten! unless @hash[name][:should].include?(value)
7777
self
7878
end

spec/elasticsearch/dsl/search/filters/bool_spec.rb

+50
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,56 @@
3232

3333
describe '#initialize' do
3434

35+
context 'when an object instance is provided' do
36+
37+
let(:search) do
38+
described_class.new.must( Elasticsearch::DSL::Search::Queries::Term.new foo: 'bar')
39+
end
40+
41+
it 'executes the block' do
42+
expect(search.to_hash).to eq(bool: { must: [ { term: { foo: 'bar' } } ] })
43+
end
44+
45+
context 'when the block calls multiple methods' do
46+
47+
let(:search) do
48+
described_class.new do
49+
must(Elasticsearch::DSL::Search::Queries::Term.new foo: 'bar')
50+
must_not(Elasticsearch::DSL::Search::Queries::Term.new moo: 'bam')
51+
should(Elasticsearch::DSL::Search::Queries::Term.new xoo: 'bax')
52+
end
53+
end
54+
55+
it 'executes the block' do
56+
expect(search.to_hash).to eq(bool:
57+
{ must: [ { term: { foo: 'bar' } } ],
58+
must_not: [ { term: { moo: 'bam' } } ],
59+
should: [ { term: { xoo: 'bax' } } ]
60+
})
61+
end
62+
end
63+
64+
context 'when the block calls multiple conditions' do
65+
66+
let(:search) do
67+
described_class.new do
68+
must(Elasticsearch::DSL::Search::Queries::Term.new foo: 'bar')
69+
must(Elasticsearch::DSL::Search::Queries::Term.new moo: 'bam')
70+
71+
should(Elasticsearch::DSL::Search::Queries::Term.new xoo: 'bax')
72+
should(Elasticsearch::DSL::Search::Queries::Term.new zoo: 'baz')
73+
end
74+
end
75+
76+
it 'executes the block' do
77+
expect(search.to_hash).to eq(bool:
78+
{ must: [ { term: { foo: 'bar' } }, { term: { moo: 'bam' } } ],
79+
should: [ { term: { xoo: 'bax' } }, { term: { zoo: 'baz' } } ]
80+
})
81+
end
82+
end
83+
end
84+
3585
context 'when a hash is provided' do
3686

3787
let(:search) do

spec/elasticsearch/dsl/search/queries/bool_spec.rb

+58
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,64 @@
3232

3333
describe '#initialize' do
3434

35+
context 'when an object instance is provided' do
36+
37+
let(:search) do
38+
described_class.new.must(Elasticsearch::DSL::Search::Queries::Match.new foo: 'bar')
39+
end
40+
41+
it 'executes the block' do
42+
expect(search.to_hash).to eq(bool: {must: [ {match: { foo: 'bar' }} ] })
43+
end
44+
45+
context 'when multiple option methods are called' do
46+
47+
let(:search) do
48+
described_class.new do
49+
should(Elasticsearch::DSL::Search::Queries::Term.new(tag: 'wow'))
50+
should(Elasticsearch::DSL::Search::Queries::Term.new(tag: 'elasticsearch'))
51+
52+
minimum_should_match 1
53+
boost 1.0
54+
end
55+
end
56+
57+
it 'defines all the options' do
58+
expect(search.to_hash).to eq(bool: {
59+
minimum_should_match: 1,
60+
boost: 1.0,
61+
should: [ {term: { tag: 'wow' }}, {term: { tag: 'elasticsearch' }} ]})
62+
end
63+
end
64+
65+
context 'when multiple conditions are provided' do
66+
67+
let(:search) do
68+
described_class.new do
69+
must(Elasticsearch::DSL::Search::Queries::Match.new foo: 'bar')
70+
must(Elasticsearch::DSL::Search::Queries::Match.new moo: 'bam')
71+
72+
should(Elasticsearch::DSL::Search::Queries::Match.new xoo: 'bax')
73+
should(Elasticsearch::DSL::Search::Queries::Match.new zoo: 'baz')
74+
end
75+
end
76+
77+
it 'applies each condition' do
78+
expect(search.to_hash).to eq(bool:
79+
{ must: [ {match: { foo: 'bar' }}, {match: { moo: 'bam' }} ],
80+
should: [ {match: { xoo: 'bax' }}, {match: { zoo: 'baz' }} ]
81+
})
82+
end
83+
84+
context 'when #to_hash is called more than once' do
85+
86+
it 'does not alter the hash' do
87+
expect(search.to_hash).to eq(search.to_hash)
88+
end
89+
end
90+
end
91+
end
92+
3593
context 'when a block is provided' do
3694

3795
let(:search) do

0 commit comments

Comments
 (0)