Collapse double negation in NotFilter#828
Open
amarkdotdev wants to merge 1 commit into
Open
Conversation
`~~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 has been signed |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #214
What
NotFilternow collapses double negation:~~filterbuilds the same query asfilter, instead of producing a nestedmust_notinside anothermust_not.Before:
After:
How
NotFilterkeeps a reference to the filter it wraps and overrides__invert__to return that wrapped filter, so inverting aNotFilterunwraps the negation rather than nesting anothermust_not.As called out in the issue, the decomposition only applies when the thing being inverted is itself a
NotFilter. Leaf filters that usemust_notinternally but are notNotFilterinstances (for exampleIsNull) 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_filterto expect the collapsed result, and added:test_not_not_not_filter— triple negation collapses to a singlemust_nottest_not_isnull_not_collapsed—IsNull(which is not aNotFilter) is not collapsedHappy to add a
CHANGELOG.rstentry once this has a PR number if that's preferred.