|
| 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 | +import static org.hamcrest.Matchers.containsString; |
| 22 | + |
| 23 | +public class RangeFieldSortIT extends ESSingleNodeTestCase { |
| 24 | + |
| 25 | + private static final String FIELD_NAME = "range"; |
| 26 | + |
| 27 | + public void testSortingOnIntegerRangeFieldThrows400() throws Exception { |
| 28 | + String indexName = "int_range_index"; |
| 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 | + containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + public void testSortingOnLongRangeFieldThrows400() throws Exception { |
| 38 | + String indexName = "long_range_index"; |
| 39 | + createIndex(indexName, FIELD_NAME, RangeType.LONG.typeName()); |
| 40 | + assertFailures( |
| 41 | + client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), |
| 42 | + RestStatus.BAD_REQUEST, |
| 43 | + containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public void testSortingOnFloatRangeFieldThrows400() throws Exception { |
| 48 | + String indexName = "float_range_index"; |
| 49 | + createIndex(indexName, FIELD_NAME, RangeType.FLOAT.typeName()); |
| 50 | + assertFailures( |
| 51 | + client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), |
| 52 | + RestStatus.BAD_REQUEST, |
| 53 | + containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public void testSortingOnDoubleRangeFieldThrows400() throws Exception { |
| 58 | + String indexName = "double_range_index"; |
| 59 | + createIndex(indexName, FIELD_NAME, RangeType.DOUBLE.typeName()); |
| 60 | + assertFailures( |
| 61 | + client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), |
| 62 | + RestStatus.BAD_REQUEST, |
| 63 | + containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public void testSortingOnIpRangeFieldThrows400() throws Exception { |
| 68 | + String indexName = "ip_range_index"; |
| 69 | + createIndex(indexName, FIELD_NAME, RangeType.IP.typeName()); |
| 70 | + assertFailures( |
| 71 | + client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), |
| 72 | + RestStatus.BAD_REQUEST, |
| 73 | + containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public void testSortingOnDateRangeFieldThrows400() throws Exception { |
| 78 | + String indexName = "date_range_index"; |
| 79 | + createIndex(indexName, FIELD_NAME, RangeType.DATE.typeName()); |
| 80 | + assertFailures( |
| 81 | + client().prepareSearch(indexName).setQuery(matchAllQuery()).addSort(SortBuilders.fieldSort(FIELD_NAME).order(SortOrder.ASC)), |
| 82 | + RestStatus.BAD_REQUEST, |
| 83 | + containsString("Sorting by range field [" + FIELD_NAME + "] is not supported") |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + private void createIndex(String indexName, String rangeFieldName, String rangeFieldType) throws Exception { |
| 88 | + int numShards = randomIntBetween(1, 3); |
| 89 | + client().admin() |
| 90 | + .indices() |
| 91 | + .prepareCreate(indexName) |
| 92 | + .setSettings(Settings.builder().put("index.number_of_shards", numShards)) |
| 93 | + .setMapping(createMapping(rangeFieldName, rangeFieldType)) |
| 94 | + .get(); |
| 95 | + } |
| 96 | + |
| 97 | + private XContentBuilder createMapping(String fieldName, String fieldType) throws Exception { |
| 98 | + return XContentFactory.jsonBuilder() |
| 99 | + .startObject() |
| 100 | + .startObject("properties") |
| 101 | + .startObject(fieldName) |
| 102 | + .field("type", fieldType) |
| 103 | + .endObject() |
| 104 | + .endObject() |
| 105 | + .endObject(); |
| 106 | + } |
| 107 | +} |
0 commit comments