fix(opensearch): don't merge same-field ranges under OR#3589
Open
chuenchen309 wants to merge 1 commit into
Open
fix(opensearch): don't merge same-field ranges under OR#3589chuenchen309 wants to merge 1 commit into
chuenchen309 wants to merge 1 commit into
Conversation
Contributor
Coverage report (pgvector)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (chroma)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (opensearch)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Contributor
Coverage report (elasticsearch)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
_parse_logical_condition and the nested-field grouping both ran
_normalize_ranges for every multi-condition group, but merging same-field
range clauses is only valid when they are AND-combined. Under OR,
`price < 10 OR price > 100` collapsed into a single {"lt": 10, "gt": 100}
range clause (meaning price < 10 AND price > 100), matching no document.
Skip range merging for OR at both the top level and inside nested-field
groups; AND and the inner `must` of NOT keep it.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
chuenchen309
force-pushed
the
fix/opensearch-or-range-merge
branch
from
July 14, 2026 10:56
7d362c9 to
84d5b6d
Compare
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.
The bug
Both the top-level
_parse_logical_conditionand the nested-field grouping call_normalize_rangesfor every multi-condition group:_normalize_rangesmerges range clauses on the same field into one (viadict.update). That's correct for AND —price > 10 AND price < 100should become one{"gt": 10, "lt": 100}range. But it also runs forOR, where it's wrong:{ "operator": "OR", "conditions": [ {"field": "meta.price", "operator": "<", "value": 10}, {"field": "meta.price", "operator": ">", "value": 100}, ], }renders as:
{"bool": {"should": [{"range": {"price": {"lt": 10, "gt": 100}}}]}}A single range clause with both
lt: 10andgt: 100meansprice < 10 AND price > 100, which matches no document — even though the user asked forprice < 10 OR price > 100. The same collapse happens inside nested-field groups (_group_nested_conditions), dropping a bound of an OR of ranges on a nested sub-field.This is a sibling of the pgvector/elasticsearch range handling; the correct output keeps two separate range clauses in
should.The fix
Only merge ranges when the conditions are AND-combined, at both sites (top level and nested groups).
ANDand the innermustofNOTkeep merging;ORskips it:Tests
Added two regression tests:
test_or_of_ranges_on_same_field_are_not_merged— top-level OR of two same-field ranges stays two separaterangeclauses.test_or_of_ranges_on_same_nested_field_are_not_merged— same, inside a nested-field group.Both fail before the change (ranges collapse into one impossible range) and pass after. I verified each fix site is load-bearing (reverting the nested guard alone makes the nested test fail).
Verified locally:
hatch run test:unitpasses (167 passed),hatch run fmtandhatch run test:typesare clean.Authored with AI assistance (Claude Code); the change and tests were reviewed and verified locally by me.