-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Add an option to force the numeric type of a field sort #38095
Conversation
This change adds an option to the `FieldSortBuilder` that allows to transform the type of a numeric field into another. Possible values for this option are `long` that transforms the source field into an integer and `double` that transforms the source field into a floating point. This new option is useful for cross-index search when the sort field is mapped differently on some indices. For instance if a field is mapped as a floating point in one index and as an integer in another it is possible to align the type for both indices using the `numeric_type` option: ``` { "sort": { "field": "my_field", "numeric_type": "double" <1> } } ``` <1> Ensure that values for this field are transformed to a floating point if needed. Only `long` and `double` are supported at the moment but the goal is to also handle `date` and `date_nanos` when elastic#32601 is merged.
Pinging @elastic/es-search |
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.
Thanks Jim, makes sense!
One thing I was wondering (not related to your PR) if it is correct that we return 503 for wrong sorting (using float and double and not setting numeric_type
)? Should it be 400 instead?
{
"error": {
"root_cause": [],
"type": "search_phase_execution_exception",
"reason": "",
"phase": "fetch",
"grouped": true,
"failed_shards": [],
"caused_by": {
"type": "class_cast_exception",
"reason": "class java.lang.Float cannot be cast to class java.lang.Double (java.lang.Float and java.lang.Double are in module java.base of loader 'bootstrap')"
}
},
"status": 503
}
|
||
Since `field` is mapped as a `double` in the first index and as a `long` | ||
in the second index, it is not possible to use this field to sort requests | ||
that query both indices bu default. However you can force the type to one |
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.
bu default -> by default
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 left some comments but the idea makes sense to me.
It is also possible to transform a floating point field into a `long` | ||
but note that in this case floating points are replaced by the largest | ||
value that is less than or equal to the argument and is equal to a mathematical | ||
integer. |
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.
this is not correct for negative values
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.
right, thanks
SortField field = fieldData.sortField(missing, localSortMode, nested, reverse); | ||
final SortField field; | ||
if (numericType != null) { | ||
if (fieldData instanceof SortedNumericDVIndexFieldData == false) { |
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.
why do you check against this class rather than IndexNumericFieldData?
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.
This is a mistake, I pushed 24c961a
This change adds an option to the `FieldSortBuilder` that allows to transform the type of a numeric field into another. Possible values for this option are `long` that transforms the source field into an integer and `double` that transforms the source field into a floating point. This new option is useful for cross-index search when the sort field is mapped differently on some indices. For instance if a field is mapped as a floating point in one index and as an integer in another it is possible to align the type for both indices using the `numeric_type` option: ``` { "sort": { "field": "my_field", "numeric_type": "double" <1> } } ``` <1> Ensure that values for this field are transformed to a floating point if needed.
This change adapts the bwc version check for the new numeric_type option of the FieldSortBuilder. Since this change needs to be backported to 7x, all bwc tests are temporary disabled until elastic#40084 is merged. Relates elastic#38095
) This change adds an option to the `FieldSortBuilder` that allows to transform the type of a numeric field into another. Possible values for this option are `long` that transforms the source field into an integer and `double` that transforms the source field into a floating point. This new option is useful for cross-index search when the sort field is mapped differently on some indices. For instance if a field is mapped as a floating point in one index and as an integer in another it is possible to align the type for both indices using the `numeric_type` option: ``` { "sort": { "field": "my_field", "numeric_type": "double" <1> } } ``` <1> Ensure that values for this field are transformed to a floating point if needed.
@jimczi I'm assuming there is nothing left to backport and removed the backport pending label. |
Looks like this went into v7.2.0? elasticsearch/server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java Lines 306 to 321 in be513ce
|
@russcam Indeed, I just fixed the version tag and the release notes. Thanks for spotting! |
This change adds an option to the
FieldSortBuilder
that allows to transform the typeof a numeric field into another. Possible values for this option are
long
that transformsthe source field into an integer and
double
that transforms the source field into a floating point.This new option is useful for cross-index search when the sort field is mapped differently on some
indices. For instance if a field is mapped as a floating point in one index and as an integer in another
it is possible to align the type for both indices using the
numeric_type
option:<1> Ensure that values for this field are transformed to a floating point if needed.
Only
long
anddouble
are supported at the moment but the goal is to also handledate
anddate_nanos
when #32601 is merged.Naming should also be improved, I choose
numeric_type
to start with something but I am not happy with it. Propositions are welcome ;).