Skip to content

Ensure integer sorts are rewritten to long sorts for BWC indexes#139293

Merged
elasticsearchmachine merged 8 commits intoelastic:mainfrom
romseygeek:bug/sort-rewrite-int-to-long
Dec 11, 2025
Merged

Ensure integer sorts are rewritten to long sorts for BWC indexes#139293
elasticsearchmachine merged 8 commits intoelastic:mainfrom
romseygeek:bug/sort-rewrite-int-to-long

Conversation

@romseygeek
Copy link
Contributor

@romseygeek romseygeek commented Dec 10, 2025

Older indexes used long sorts for integer fields, and we need to ensure that
sorts against these fields do not conflict. Recent refactoring in
IndexNumericSortField missed a case where this could happen.

Closes #139127
Closes #139128

@romseygeek romseygeek self-assigned this Dec 10, 2025
@romseygeek romseygeek added >non-issue :Search/Search Search-related issues that do not fall into other categories v9.3.0 labels Dec 10, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Search Meta label for search team label Dec 10, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search (Team:Search)

@john-wagster
Copy link
Contributor

Fixes: #139293

@benwtrent benwtrent added >bug and removed >non-issue labels Dec 10, 2025
@elasticsearchmachine
Copy link
Collaborator

Hi @romseygeek, I've created a changelog YAML for you.

@benwtrent
Copy link
Member

@romseygeek I think this might also fix this issue: #139275

We saw this NPE on an index created before (thus sorting on long) and then sorting with a new index (sorting on int).

@benwtrent
Copy link
Member

Consequently, what do you think about backporting to 9.2.x (and maybe 9.1.x?)?

@romseygeek
Copy link
Contributor Author

This specific bug is only in 9.3 I think, although the fix may also clean up other issues. There have been several changes in IndexNumericFieldData which means this won't be trivial to backport.

@romseygeek
Copy link
Contributor Author

which means this won't be trivial to backport.

To be clear, that doesn't mean we shouldn't do it! Just that it probably needs to be a separate PR and a different test setup.

return FieldSortBuilder.fromXContent(parser, fieldName);
}

public void testIntRewritesToLong() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

very nice test!

);

XFieldComparatorSource longSource = comparatorSource(NumericType.LONG, missingValue, sortMode, nested);
if (sortField instanceof SortedNumericSortField snsf) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For my education, under what condition index sort is not instance of SortedNumericSortField?

Is it something new?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Index sorts are always SortedNumericSortField instances, but query-time sorts now generally use custom comparators to pick up some performance improvements that are not in a released lucene version, so they are plain SortField instances.

Copy link
Contributor

@mayya-sharipova mayya-sharipova left a comment

Choose a reason for hiding this comment

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

@romseygeek Thanks for a fix!

@mayya-sharipova
Copy link
Contributor

I think this might also fix this issue: #139275

This specific issue originally involved scroll, so I am not sure.
I will need to check this.

@romseygeek romseygeek added the auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) label Dec 11, 2025
@romseygeek
Copy link
Contributor Author

I'll backport the test to 9.2 and see if it surfaces any issues.

@elasticsearchmachine elasticsearchmachine merged commit cba3128 into elastic:main Dec 11, 2025
34 checks passed
@romseygeek
Copy link
Contributor Author

I opened #139358 to backport the tests, which seem to pass at the moment so I think #139275 must be a different issue.

@romseygeek romseygeek deleted the bug/sort-rewrite-int-to-long branch December 11, 2025 10:24
szybia added a commit to szybia/elasticsearch that referenced this pull request Dec 11, 2025
* upstream/main: (79 commits)
  Mute org.elasticsearch.test.rest.yaml.CcsCommonYamlTestSuiteIT test {p0=search/140_pre_filter_search_shards/prefilter on non-indexed date fields} elastic#139381
  Adjust error bounds for bfloat16 value checks (elastic#139371)
  Unmute some vector CSS tests (elastic#139370)
  Do not allow `project_routing` as a query param (elastic#139206)
  Unmute HalfFloat...Tests#testSynthesizeArrayRandom (elastic#139341)
  Remove leniency in LinkedProjectConfig builder methods (elastic#139012)
  EQL: fix project_routing (elastic#139366)
  Add patch version for 9.2 index version constant (elastic#139362)
  Mute org.elasticsearch.test.rest.yaml.RcsCcsCommonYamlTestSuiteIT test {p0=search.vectors/200_dense_vector_docvalue_fields/dense_vector docvalues with bfloat16} elastic#139368
  ES|QL: Enable CCS tests for FORK (elastic#139302)
  Restructuring the semantic_text field type page  (elastic#138571)
  AggregateMetricDouble fields should not build BKD indexes (elastic#138724)
  Feature/count by trunc with filter (elastic#138765)
  ESQL: Convert TS 500 error to 400 (elastic#139360)
  [CI] Rerun failing tests for periodic build pipelines (elastic#139200)
  revert muting saml test (elastic#139327)
  Add TDigest histogram as metric (elastic#139247)
  Links solved bugs to class cast exception changelog and unmutes errors (elastic#139340)
  Ensure integer sorts are rewritten to long sorts for BWC indexes (elastic#139293)
  Integrate stored fields format bloom filter with synthetic _id (elastic#138515)
  ...
romseygeek added a commit that referenced this pull request Dec 12, 2025
romseygeek added a commit to romseygeek/elasticsearch that referenced this pull request Dec 15, 2025
Sorts against integer fields in pre-8.19 indexes are rewritten to use
LongComparators to stay consistent with pre-written index metadata.
These also need to always expect multi-valued fields.  elastic#139293
incorrectly built single-valued sorts in certain circumstances, which
would then throw runtime errors.
romseygeek added a commit that referenced this pull request Dec 16, 2025
Sorts against integer fields in pre-8.19 indexes are rewritten to use
LongComparators to stay consistent with pre-written index metadata.
These also need to always expect multi-valued fields.  #139293
incorrectly built single-valued sorts in certain circumstances, which
would then throw runtime errors.
romseygeek added a commit to romseygeek/elasticsearch that referenced this pull request Dec 17, 2025
…39538)

Sorts against integer fields in pre-8.19 indexes are rewritten to use
LongComparators to stay consistent with pre-written index metadata.
These also need to always expect multi-valued fields.  elastic#139293
incorrectly built single-valued sorts in certain circumstances, which
would then throw runtime errors.
romseygeek added a commit to romseygeek/elasticsearch that referenced this pull request Dec 17, 2025
…39538)

Sorts against integer fields in pre-8.19 indexes are rewritten to use
LongComparators to stay consistent with pre-written index metadata.
These also need to always expect multi-valued fields.  elastic#139293
incorrectly built single-valued sorts in certain circumstances, which
would then throw runtime errors.
elasticsearchmachine pushed a commit that referenced this pull request Dec 17, 2025
…139700)

Sorts against integer fields in pre-8.19 indexes are rewritten to use 
LongComparators to stay consistent with pre-written index metadata. 
These also need to always expect multi-valued fields.  #139293
incorrectly  built single-valued sorts in certain circumstances, which
would then  throw runtime errors.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-merge-without-approval Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) >bug :Search/Search Search-related issues that do not fall into other categories Team:Search Meta label for search team v9.3.0

Projects

None yet

5 participants