Preserve constant_score through filtered aliases - #227
Open
DarshitChanpura wants to merge 7 commits into
Open
DarshitChanpura wants to merge 7 commits into
DarshitChanpura wants to merge 7 commits into
Conversation
DarshitChanpura
force-pushed
the
feat/constant-score-filtered-query
branch
from
August 19, 2026 16:42
96e9faf to
0073302
Compare
When a search targets a filtered alias, DefaultSearchContext.buildFilteredQuery adds the alias filter by wrapping the user's query in a scoring BooleanQuery (bool[query MUST, aliasFilter FILTER]). If the user's query was a ConstantScoreQuery, nesting it inside that scoring boolean re-introduces scoring of the inner query and defeats Lucene's COMPLETE_NO_SCORES fast path, so the inner query's scorer is set up over the whole postings list. Re-wrap the filtered result in a ConstantScoreQuery when the main query was already constant-scored, keeping the outermost query non-scoring. This preserves both the no-scoring contract and the fast path end-to-end. Matters for access-control filters (e.g. document-level security composed with a filtered alias), where the combined query would otherwise be scored despite the caller having requested constant scoring. Verified by ConstantScoreFilteredAliasIT (hits score 1.0 through the alias, matching the backing index); IndexAliasesIT (29/29) confirms no regression for normal scored alias queries. Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
DarshitChanpura
force-pushed
the
feat/constant-score-filtered-query
branch
from
August 19, 2026 20:00
0073302 to
6c9c337
Compare
…ect#22765) Signed-off-by: Liyun Xiu <xiliyun@amazon.com> Co-authored-by: Andriy Redko <drreta@gmail.com>
…-project#22778) Signed-off-by: Peter Zhu <zhujiaxi@amazon.com>
…earch-project#22493) collectDocs now returns packed i64: upper 32 bits = next matching docId beyond maxDoc, lower 32 bits = wordsWritten. No new FFM calls needed. Rust consumers use nextDoc for three levels of optimization: 1. Full RG skip: nextDoc >= rgMax → no collectDocs call 2. Tightened range: nextDoc > rgMin → start from nextDoc, smaller bitset 3. No benefit: nextDoc <= rgMin → call as before Implemented in both SingleCollectorEvaluator (AtomicI32 across RGs) and CollectorLeafBitmaps/BitmapTree (per-leaf HashMap keyed by Arc identity). Signed-off-by: Somesh Gupta <someshgupta987@gmail.com>
…ect#22704) * Remove Jackson 2.x dependencies from OpenSearch core Signed-off-by: Andriy Redko <drreta@gmail.com> * Address review comments Signed-off-by: Andriy Redko <drreta@gmail.com> --------- Signed-off-by: Andriy Redko <drreta@gmail.com>
Covers both branches of the ConstantScoreQuery re-wrap in DefaultSearchContext.buildFilteredQuery: a ConstantScoreQuery input with a filter present stays non-scoring (returns a ConstantScoreQuery), and a normal query is combined into a scoring BooleanQuery. Complements the existing ConstantScoreFilteredAliasIT with method-level coverage. Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
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.
What
When a search targets a filtered alias,
DefaultSearchContext.buildFilteredQueryadds the alias filter by wrapping the user's query in a scoring
BooleanQuery(
bool[query MUST, aliasFilter FILTER]). If the user's query was aConstantScoreQuery, nesting it inside that scoring boolean:"no scoring" request — hits no longer score a constant), and
COMPLETE_NO_SCORESfast path, so the inner query's scoreris set up over the whole postings list.
The second point is a real latency cliff: a
constant_score(match: <frequent term>)that runs in ~0 ms against a concrete index takes ~15 ms through a filtered alias
at scale, purely because the alias filter re-wraps it in a scoring boolean.
Fix
Re-wrap the filtered result in a
ConstantScoreQuerywhen the main query wasalready constant-scored, so the outermost query stays non-scoring. This preserves
both the no-scoring contract and the fast path end-to-end.
Only the constant-score path is affected; normal scored queries on aliases are
unchanged.
Testing
ConstantScoreFilteredAliasIT— aconstant_scorequery scores every hitexactly 1.0 through a filtered alias, identical to the backing index (before
the fix the alias path produced varying BM25 scores).
IndexAliasesIT(29/29) — no regression for normal scored alias queries.