Skip to content

Commit f856f5d

Browse files
authored
Inverse of an empty Bool() should be MatchNone()
1 parent 99b787c commit f856f5d

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

elasticsearch_dsl/query.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ def _min_should_match(self):
180180
)
181181

182182
def __invert__(self):
183+
# Because an empty Bool query is treated like
184+
# MatchAll the inverse should be MatchNone
185+
if not any(chain(self.must, self.filter, self.should, self.must_not)):
186+
return MatchNone()
187+
183188
negations = []
184189
for q in chain(self.must, self.filter):
185190
negations.append(~q)

tests/test_query.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ def test_not_match_none_is_match_all():
206206
assert ~q == query.MatchAll()
207207

208208

209+
def test_invert_empty_bool_is_match_none():
210+
q = query.Bool()
211+
212+
assert ~q == query.MatchNone()
213+
214+
209215
def test_match_none_or_query_equals_query():
210216
q1 = query.Match(f=42)
211217
q2 = query.MatchNone()

0 commit comments

Comments
 (0)