Smarter use of sort-field optimizations in DV range queries - #16424
Merged
Conversation
When the primary sort field on a segment contains no values or only a single value, then it is effectively a no-op and range queries against the secondary sort field can make use of the sort-field optimizations usually reserved for queries against the primary. This adds a helper method, Sort.getPrimarySortField(LeafReader), that tries to identify no-op primary sort fields using index-level stats from FieldInfo or associated DocValuesSkipper entries, and uses it in the various XDocValuesRangeQuery implementations.
Contributor
Author
|
We can also use this in TopFieldCollector, but that's slightly more complex so I'd like to leave it for a follow-up. |
sgup432
reviewed
Jul 28, 2026
| // If the field has a skip index, check whether all values are identical, | ||
| // in which case sorting by this field is a no-op for this segment. | ||
| DocValuesSkipper skipper = reader.getDocValuesSkipper(field); | ||
| if (skipper != null && skipper.minValue() == skipper.maxValue()) { |
Contributor
There was a problem hiding this comment.
I believe we should also check for dense fields here i.e. every doc has a value for that field?
As for sparse field, missing values on a primary sort field does not mean that secondary sort is globally sorted?
Contributor
Author
There was a problem hiding this comment.
Oh that's a good point - if we have docs with missing values then that adds an implicit second sort value. We currently check for denseness in the top-level caller but we should do that here instead. I'll update, thanks!
sgup432
reviewed
Jul 28, 2026
sgup432
left a comment
Contributor
There was a problem hiding this comment.
A smart optimization indeed! 😄
romseygeek
added a commit
that referenced
this pull request
Jul 30, 2026
When the primary sort field on a segment contains no values or only a single value, then it is effectively a no-op and range queries against the secondary sort field can make use of the sort-field optimizations usually reserved for queries against the primary. This adds a helper method, Sort.getPrimarySortField(LeafReader), that tries to identify no-op primary sort fields using index-level stats from FieldInfo or associated DocValuesSkipper entries, and uses it in the various XDocValuesRangeQuery implementations.
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.
When the primary sort field on a segment contains no values or only a single value, then it is effectively a no-op and range queries against the secondary sort field can make use of the sort-field optimizations usually reserved for queries against the primary.
This adds a helper method, Sort.getPrimarySortField(LeafReader), that tries to identify no-op primary sort fields using index-level stats from FieldInfo or associated DocValuesSkipper entries, and uses it in the various XDocValuesRangeQuery implementations.