Skip to content

[MOD-17353] Rust numeric top k: don't use emitted: HashSet<DocId> if the index is not for multi value fields - #10746

Merged
ThierryBerger merged 2 commits into
masterfrom
thierry-17353
Sep 23, 2026
Merged

ThierryBerger merged 2 commits into
masterfrom
thierry-17353

Conversation

@ThierryBerger

@ThierryBerger ThierryBerger commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Local benchmark

benchmark change
filtered n10k k10 −24.2%
filtered n10k k100 −21.6%
filtered n100k k10 −21.6%
filtered n100k k100 −20.1%
unfiltered n10k k10 −29.4%
unfiltered n10k k100 −28.2%
unfiltered n100k k10 −29.0%
unfiltered n100k k100 −29.4%
selectivity 1% −27.8%
selectivity 10% −20.1%
selectivity 50% −18.4%
expensive child 1% −27.3%
expensive child 10% −12.9%
expensive child 50% −8.3%

options

We can approach this in different ways: the cause of multiple docs being returned is when we use multi-value fields:

  • We can check for the index if its supports multi value fields.
  • We can check each entered documents to know for sure if a document is multi-valued or not. This way a configuration using the capability of multi value field doesn't pay the price of its more expensive algorithm.
    • I took this option, let me know if that's too invasive.

Mark if applicable

  • This PR introduces API changes
  • This PR introduces serialization changes

Release Notes

  • This PR requires release notes
  • This PR does not require release notes

If a release note is required (bug fix / new feature / enhancement), describe the user impact of this PR in the title.

@ThierryBerger
ThierryBerger changed the base branch from master to thierry-mod-14945 August 4, 2026 15:42
@ThierryBerger
ThierryBerger changed the base branch from thierry-mod-14945 to thierry-14948 August 4, 2026 15:56
@ThierryBerger ThierryBerger mentioned this pull request Aug 4, 2026
1 of 4 tasks
@chatgpt-codex-connector

Copy link
Copy Markdown

💡 Codex Review

self.last_limit_estimate = ((estimate as f64) * success_ratio) as usize;

P2 Badge Size retry windows from observed selectivity

When a filtered numeric SORTBY drains a window with heap_count < k and a non-tiny hit rate, this multiplies the original selectivity-based estimate by success_ratio, which shrinks the next window exactly when the observed hit rate says it should grow. For example, if the first window hits 25% while the child estimate assumed 50%, a query missing 50 results asks for roughly 25 docs instead of the ~200 needed, causing extra child rewinds/intersections and then an unbounded fallback for expensive filters. The C optimizer recalculates from the observed ratio (successRatio * numDocs) before estimating the next limit; use the same observed-selectivity calculation here.


if self.child.is_some() && self.source.yields_child_record() {

P2 Badge Skip child-record capture for source-built results

When yields_child_record() is false (the new numeric SORTBY path), this branch avoids returning the child record only after collection, but intersect_batch_with_child still unconditionally deep-copies child.current() for every matching candidate before pushing it into the heap. For filtered numeric sorts over text/tag children those captured records are discarded here and never used, so the optimizer still pays offset cloning and lifetime bookkeeping that this opt-out was meant to avoid. Thread the flag into the intersection path and pass None when the source will build its own result.


items.sort_unstable_by_key(|(doc_id, _)| *doc_id);

P2 Badge Check the deadline after sorting a large batch

When a numeric range batch is large enough that sort_unstable_by_key takes noticeable time, the deadline can expire during this sort, but the function returns the batch and the caller then performs the full heap/intersection pass before the next source timeout check. This makes timed-out numeric SORTBY queries continue doing substantial extra work after the ordering pass; add a check_timeout() immediately after the sort (and before coalescing/returning) so expiration during the sort is observed before more collection work starts.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.35294% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.56%. Comparing base (8d1dae1) to head (4ef4848).
⚠️ Report is 8 commits behind head on master.

Files with missing lines Patch % Lines
...arch_rs/numeric_score_source/src/range_iterator.rs 75.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10746      +/-   ##
==========================================
- Coverage   84.22%   83.56%   -0.66%     
==========================================
  Files         639      665      +26     
  Lines       85147    86494    +1347     
  Branches    39083    40302    +1219     
==========================================
+ Hits        71711    72281     +570     
- Misses      13072    13838     +766     
- Partials      364      375      +11     
Flag Coverage Δ
flow 84.71% <ø> (+0.01%) ⬆️
unit 58.74% <82.35%> (-0.11%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7faf80db1a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@sonarqubecloud

Copy link
Copy Markdown

Base automatically changed from thierry-14948 to master August 17, 2026 12:31
@github-actions github-actions Bot added size:S and removed size:M labels Sep 22, 2026
Ranges are value-disjoint, so a document with a single value occurs in exactly
one range and cannot be reached twice by a value-ordered scan. Track whether the
tree holds any multivalued document, and skip the per-record emitted-set lookup
when it does not
GuyAv46
GuyAv46 previously approved these changes Sep 23, 2026

@GuyAv46 GuyAv46 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised we didn't have this flag before, and that is_multivalued is only an input for the numeric add logic.

last_doc_id: DocId,
/// Whether any document has been indexed under more than one value, as a
/// multivalue field does. See [`has_multivalued_docs`](Self::has_multivalued_docs).
has_multivalued_docs: bool,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth mentioning that this flag is currently never cleared? Once we see a multi-value, it will never turn off

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a mention on the public getter has_multivalued_docs

@ThierryBerger

Copy link
Copy Markdown
Collaborator Author

I'm surprised we didn't have this flag before

Well, we have a flag in the index, that's passed through is_multivalued, but it's about "capability", not actual data: if the index has the multi value field capability but doesn't use it, we end up taking the "slow" path for no improvement.

We can argue it's a user error, but I figured it's inexpensive enough to carry a bool as we go through it to have a more accurate information.

I don't know if has_multivalued_docs is relevant to be used for another need.

@ThierryBerger
ThierryBerger added this pull request to the merge queue Sep 23, 2026
@sonarqubecloud

Copy link
Copy Markdown

Merged via the queue into master with commit 7060a99 Sep 23, 2026
61 checks passed
@ThierryBerger
ThierryBerger deleted the thierry-17353 branch September 23, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants