-
Notifications
You must be signed in to change notification settings - Fork 25.6k
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
Merged
drempapis
merged 11 commits into
elastic:main
from
drempapis:sorting_on_range_fields_throws_400
Jul 2, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7388cab
Add fix and test
drempapis cdabfb7
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis e0ef65b
Update docs/changelog/129725.yaml
drempapis aa7f66f
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis 0a7d147
update after review
drempapis 22977dc
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis ee60573
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis c725a42
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis a270840
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis 28b0b44
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis 77055d8
Merge branch 'main' into sorting_on_range_fields_throws_400
drempapis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 129725 | ||
| summary: Throw a 400 when sorting for all types of range fields | ||
| area: Search | ||
| type: bug | ||
| issues: [] |
107 changes: 107 additions & 0 deletions
107
server/src/internalClusterTest/java/org/elasticsearch/search/sort/RangeFieldSortIT.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.sort; | ||
|
|
||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.index.mapper.RangeType; | ||
| import org.elasticsearch.rest.RestStatus; | ||
| import org.elasticsearch.test.ESSingleNodeTestCase; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
|
|
||
| import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; | ||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures; | ||
| import static org.hamcrest.Matchers.containsString; | ||
|
|
||
| public class RangeFieldSortIT extends ESSingleNodeTestCase { | ||
|
|
||
| private static final String FIELD_NAME = "range"; | ||
|
|
||
| public void testSortingOnIntegerRangeFieldThrows400() throws Exception { | ||
| String indexName = "int_range_index"; | ||
| createIndex(indexName, FIELD_NAME, RangeType.INTEGER.typeName()); | ||
| assertFailures( | ||
| client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), | ||
| RestStatus.BAD_REQUEST, | ||
| containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") | ||
| ); | ||
| } | ||
|
|
||
| public void testSortingOnLongRangeFieldThrows400() throws Exception { | ||
| String indexName = "long_range_index"; | ||
| createIndex(indexName, FIELD_NAME, RangeType.LONG.typeName()); | ||
| assertFailures( | ||
| client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), | ||
| RestStatus.BAD_REQUEST, | ||
| containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") | ||
| ); | ||
| } | ||
|
|
||
| public void testSortingOnFloatRangeFieldThrows400() throws Exception { | ||
| String indexName = "float_range_index"; | ||
| createIndex(indexName, FIELD_NAME, RangeType.FLOAT.typeName()); | ||
| assertFailures( | ||
| client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), | ||
| RestStatus.BAD_REQUEST, | ||
| containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") | ||
| ); | ||
| } | ||
|
|
||
| public void testSortingOnDoubleRangeFieldThrows400() throws Exception { | ||
| String indexName = "double_range_index"; | ||
| createIndex(indexName, FIELD_NAME, RangeType.DOUBLE.typeName()); | ||
| assertFailures( | ||
| client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), | ||
| RestStatus.BAD_REQUEST, | ||
| containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") | ||
| ); | ||
| } | ||
|
|
||
| public void testSortingOnIpRangeFieldThrows400() throws Exception { | ||
| String indexName = "ip_range_index"; | ||
| createIndex(indexName, FIELD_NAME, RangeType.IP.typeName()); | ||
| assertFailures( | ||
| client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), | ||
| RestStatus.BAD_REQUEST, | ||
| containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") | ||
| ); | ||
| } | ||
|
|
||
| public void testSortingOnDateRangeFieldThrows400() throws Exception { | ||
| String indexName = "date_range_index"; | ||
| createIndex(indexName, FIELD_NAME, RangeType.DATE.typeName()); | ||
| assertFailures( | ||
| client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), | ||
| RestStatus.BAD_REQUEST, | ||
| containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") | ||
| ); | ||
| } | ||
|
|
||
| private void createIndex(String indexName, String rangeFieldName, String rangeFieldType) throws Exception { | ||
| int numShards = randomIntBetween(1, 3); | ||
| client().admin() | ||
| .indices() | ||
| .prepareCreate(indexName) | ||
| .setSettings(Settings.builder().put("index.number_of_shards", numShards)) | ||
| .setMapping(createMapping(rangeFieldName, rangeFieldType)) | ||
| .get(); | ||
| } | ||
|
|
||
| private XContentBuilder createMapping(String fieldName, String fieldType) throws Exception { | ||
| return XContentFactory.jsonBuilder() | ||
| .startObject() | ||
| .startObject("properties") | ||
| .startObject(fieldName) | ||
| .field("type", fieldType) | ||
| .endObject() | ||
| .endObject() | ||
| .endObject(); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
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!