Skip to content

Commit 7388cab

Browse files
committed
Add fix and test
1 parent c173522 commit 7388cab

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.search.sort;
11+
12+
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.index.mapper.RangeType;
14+
import org.elasticsearch.rest.RestStatus;
15+
import org.elasticsearch.test.ESSingleNodeTestCase;
16+
import org.elasticsearch.xcontent.XContentBuilder;
17+
import org.elasticsearch.xcontent.XContentFactory;
18+
19+
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
20+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures;
21+
22+
public class RangeFieldSortIT extends ESSingleNodeTestCase {
23+
24+
private static final String FIELD_NAME = "range";
25+
26+
public void testSortingOnIntegerRangeFieldThrows400() throws Exception {
27+
String indexName = "int_range_index";
28+
;
29+
createIndex(indexName, FIELD_NAME, RangeType.INTEGER.typeName());
30+
assertFailures(
31+
client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)),
32+
RestStatus.BAD_REQUEST
33+
);
34+
}
35+
36+
public void testSortingOnLongRangeFieldThrows400() throws Exception {
37+
String indexName = "long_range_index";
38+
createIndex(indexName, FIELD_NAME, RangeType.LONG.typeName());
39+
assertFailures(
40+
client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)),
41+
RestStatus.BAD_REQUEST
42+
);
43+
}
44+
45+
public void testSortingOnFloatRangeFieldThrows400() throws Exception {
46+
String indexName = "float_range_index";
47+
createIndex(indexName, FIELD_NAME, RangeType.FLOAT.typeName());
48+
assertFailures(
49+
client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)),
50+
RestStatus.BAD_REQUEST
51+
);
52+
}
53+
54+
public void testSortingOnDoubleRangeFieldThrows400() throws Exception {
55+
String indexName = "double_range_index";
56+
createIndex(indexName, FIELD_NAME, RangeType.DOUBLE.typeName());
57+
assertFailures(
58+
client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)),
59+
RestStatus.BAD_REQUEST
60+
);
61+
}
62+
63+
public void testSortingOnIpRangeFieldThrows400() throws Exception {
64+
String indexName = "ip_range_index";
65+
createIndex(indexName, FIELD_NAME, RangeType.IP.typeName());
66+
assertFailures(
67+
client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)),
68+
RestStatus.BAD_REQUEST
69+
);
70+
}
71+
72+
public void testSortingOnDateRangeFieldThrows400() throws Exception {
73+
String indexName = "date_range_index";
74+
createIndex(indexName, FIELD_NAME, RangeType.DATE.typeName());
75+
assertFailures(
76+
client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)),
77+
RestStatus.BAD_REQUEST
78+
);
79+
}
80+
81+
private void createIndex(String indexName, String rangeFieldName, String rangeFieldType) throws Exception {
82+
int numShards = randomIntBetween(1, 3);
83+
client().admin()
84+
.indices()
85+
.prepareCreate(indexName)
86+
.setSettings(Settings.builder().put("index.number_of_shards", numShards))
87+
.setMapping(createMapping(rangeFieldName, rangeFieldType))
88+
.get();
89+
}
90+
91+
private XContentBuilder createMapping(String fieldName, String fieldType) throws Exception {
92+
return XContentFactory.jsonBuilder()
93+
.startObject()
94+
.startObject("properties")
95+
.startObject(fieldName)
96+
.field("type", fieldType)
97+
.endObject()
98+
.endObject()
99+
.endObject();
100+
}
101+
}

server/src/main/java/org/elasticsearch/search/sort/FieldSortBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.index.mapper.NestedLookup;
3535
import org.elasticsearch.index.mapper.NestedObjectMapper;
3636
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberFieldType;
37+
import org.elasticsearch.index.mapper.RangeFieldMapper;
3738
import org.elasticsearch.index.query.QueryBuilder;
3839
import org.elasticsearch.index.query.QueryRewriteContext;
3940
import org.elasticsearch.index.query.QueryShardException;
@@ -347,6 +348,11 @@ public SortFieldAndFormat build(SearchExecutionContext context) throws IOExcepti
347348
}
348349

349350
MappedFieldType fieldType = context.getFieldType(fieldName);
351+
352+
if (fieldType instanceof RangeFieldMapper.RangeFieldType) {
353+
throw new IllegalArgumentException("Sorting by range field [" + fieldName + "] is not supported");
354+
}
355+
350356
Nested nested = nested(context, fieldType);
351357
if (fieldType == null) {
352358
fieldType = resolveUnmappedType(context);

0 commit comments

Comments
 (0)