Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions eland/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ def __init__(self, *args: BooleanFilter) -> None:
class NotFilter(BooleanFilter):
def __init__(self, x: BooleanFilter) -> None:
super().__init__()
self._x = x
self._filter = {"bool": {"must_not": x.build()}}

def __invert__(self) -> "BooleanFilter":
# Double negation cancels out: ``~~filter`` is equivalent to ``filter``,
# so return the wrapped filter instead of nesting another ``must_not``.
# This only applies to ``NotFilter`` itself; leaf filters that merely
# use ``must_not`` internally (e.g. ``IsNull``) are unaffected.
return self._x


# LeafBooleanFilter
class GreaterEqual(BooleanFilter):
Expand Down
13 changes: 12 additions & 1 deletion tests/operators/test_operators_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,21 @@ def test_not_filter(self):
assert exp.build() == {"bool": {"must_not": {"range": {"a": {"gte": 2}}}}}

def test_not_not_filter(self):
# Double negation cancels out: ~~filter is equivalent to filter.
exp = ~~GreaterEqual("a", 2)
assert exp.build() == {"range": {"a": {"gte": 2}}}

def test_not_not_not_filter(self):
# An odd number of negations collapses to a single negation.
exp = ~~~GreaterEqual("a", 2)
assert exp.build() == {"bool": {"must_not": {"range": {"a": {"gte": 2}}}}}

def test_not_isnull_not_collapsed(self):
# IsNull uses must_not internally but is not a NotFilter, so inverting
# it must not be treated as a double negation.
exp = ~IsNull("a")
assert exp.build() == {
"bool": {"must_not": {"bool": {"must_not": {"range": {"a": {"gte": 2}}}}}}
"bool": {"must_not": {"bool": {"must_not": {"exists": {"field": "a"}}}}}
}

def test_not_and_filter(self):
Expand Down