Skip to content

Collapse double negation in NotFilter#828

Open
amarkdotdev wants to merge 1 commit into
elastic:mainfrom
amarkdotdev:optimize-notfilter-double-negation
Open

Collapse double negation in NotFilter#828
amarkdotdev wants to merge 1 commit into
elastic:mainfrom
amarkdotdev:optimize-notfilter-double-negation

Conversation

@amarkdotdev

Copy link
Copy Markdown

Closes #214

What

NotFilter now collapses double negation: ~~filter builds the same query as filter, instead of producing a nested must_not inside another must_not.

Before:

(~~GreaterEqual("a", 2)).build()
# {"bool": {"must_not": {"bool": {"must_not": {"range": {"a": {"gte": 2}}}}}}}

After:

(~~GreaterEqual("a", 2)).build()
# {"range": {"a": {"gte": 2}}}

How

NotFilter keeps a reference to the filter it wraps and overrides __invert__ to return that wrapped filter, so inverting a NotFilter unwraps the negation rather than nesting another must_not.

As called out in the issue, the decomposition only applies when the thing being inverted is itself a NotFilter. Leaf filters that use must_not internally but are not NotFilter instances (for example IsNull) are left untouched, so ~IsNull("a") still produces the expected nested form.

An odd number of negations still collapses correctly to a single negation (~~~x == ~x).

Tests

Updated test_not_not_filter to expect the collapsed result, and added:

  • test_not_not_not_filter — triple negation collapses to a single must_not
  • test_not_isnull_not_collapsedIsNull (which is not a NotFilter) is not collapsed

Happy to add a CHANGELOG.rst entry once this has a PR number if that's preferred.

`~~filter` now builds the same query as `filter` instead of nesting two
`must_not` clauses. NotFilter keeps a reference to the filter it wraps and
overrides `__invert__` to return it, so inverting a NotFilter unwraps the
negation rather than adding another one.

Only NotFilter itself is unwrapped; leaf filters that use `must_not`
internally (such as IsNull) are unaffected.

Closes elastic#214
@cla-checker-service

cla-checker-service Bot commented Jun 28, 2026

Copy link
Copy Markdown

💚 CLA has been signed

@prodsecmachine

prodsecmachine commented Jun 28, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@amarkdotdev amarkdotdev reopened this Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize NotFilter to cancel out when combined with another NotFilter

2 participants