-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Throw a 400 when sorting for all types of range fields #129725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw a 400 when sorting for all types of range fields #129725
Conversation
Hi @drempapis, I've created a changelog YAML for you. |
Pinging @elastic/es-search-foundations (Team:Search Foundations) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need to update the docs as well to highlight this discrepancy, I believe the sorting page does not specify that certain field types don't support sorting.
|
||
if (fieldType instanceof RangeFieldMapper.RangeFieldType) { | ||
throw new IllegalArgumentException("Sorting by range field [" + fieldName + "] is not supported"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do we do the same for other non sortable field types? Are there ways to avoid an instance of check perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, @javanna. You're right, using instanceof
can lead to fragile and hard-to-maintain code in certain cases. In this instance, my approach was primarily influenced by the method of application, as instanceof is used in several places throughout the code.
Following the behavior observed with geo_point, where calling AbstractPointIndexFieldData#sortField results in an IllegalArgumentException, I updated the RangeFieldMapper#fielddataBuilder implementation. Specifically, we now return an IndexFieldData.Builder that overrides the sortField method to customize its behavior for all range types.
@javanna, I was considering updating the documentation in a separate PR. Would it be preferable to include it as part of this one instead?" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, assuming @iverase is onboard with it too
Thanks for fixing this!
XFieldComparatorSource.Nested nested, | ||
boolean reverse | ||
) { | ||
throw new IllegalArgumentException("Sorting by range field [" + name() + "] is not supported"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this solution, much better than the instance of. Those should be removed rather than being a precedent for adding more over time :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fully agree, thank you for the review!
In main, sorting on Range Fields Triggers AssertionError and Unexpected Behavior in ES.
When assertions are enabled, attempting to sort on certain range field types, such as
integer_range
,long_range
,float_range
,double_range
, orip_range
, results in a400
. This occurs due to asearch_phase_execution_exception
caused by anillegal_argument_exception
, which is ultimately rooted in anAssertionError
during Lucene's byte-to-UTF8 formatting.This issue occurs when the code attempts to format a
BytesRef
value returned from a range field sort as a human-readable string. Still, the operation fails because the data is not intended to be interpreted asUTF-8
. The relevant assertion expects sort values to be of known types (String, Number, Boolean, or Map) and throws when raw bytes are encountered instead.When assertions are disabled, the sort operation
completes
without failure; however, it returns non-human-readable byte sequences (e.g.,"\u0001��"
) as sort values. This behavior is considered unreliable and unsuitable for use in production environments.For the
date_range
field type, the behavior is slightly different. When assertions are enabled, execution fails with the following error:java.lang.AssertionError: [1 a9 94 1f 29 7c 0 a9 9b 76 da a7 ff] was not formatted
.When assertions are disabled, and the
size
parameter restricts the number of returned documents to fewer than the total available, a500
Internal Server Error is triggered, caused by anunsupported_operation_exception
.This PR disables sorting for all
range field
types, aligning their behavior with other non-sortable field types (e.g., geo types). When a sort operation is attempted on a range field, Elasticsearch now returns a400
response with a clear message indicating that sorting is not supported for the given field, which is of type range field.solves #122358