Skip to content

Smarter use of sort-field optimizations in DV range queries - #16424

Merged
romseygeek merged 5 commits into
apache:mainfrom
romseygeek:sort/get-primary-sortfield
Jul 30, 2026
Merged

Smarter use of sort-field optimizations in DV range queries#16424
romseygeek merged 5 commits into
apache:mainfrom
romseygeek:sort/get-primary-sortfield

Conversation

@romseygeek

Copy link
Copy Markdown
Contributor

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.

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.
@romseygeek

Copy link
Copy Markdown
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.

// 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()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 sgup432 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A smart optimization indeed! 😄

@sgup432 sgup432 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@romseygeek
romseygeek merged commit 693a723 into apache:main Jul 30, 2026
12 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants