|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.fieldcaps; |
| 21 | + |
| 22 | +import org.elasticsearch.action.fieldcaps.FieldCapabilities; |
| 23 | +import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 26 | +import org.elasticsearch.test.ESIntegTestCase; |
| 27 | +import org.junit.Before; |
| 28 | + |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 32 | + |
| 33 | +public class FieldCapabilitiesIT extends ESIntegTestCase { |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() throws Exception { |
| 37 | + super.setUp(); |
| 38 | + |
| 39 | + XContentBuilder oldIndexMapping = XContentFactory.jsonBuilder() |
| 40 | + .startObject() |
| 41 | + .startObject("_doc") |
| 42 | + .startObject("properties") |
| 43 | + .startObject("distance") |
| 44 | + .field("type", "double") |
| 45 | + .endObject() |
| 46 | + .startObject("route_length_miles") |
| 47 | + .field("type", "alias") |
| 48 | + .field("path", "distance") |
| 49 | + .endObject() |
| 50 | + .endObject() |
| 51 | + .endObject() |
| 52 | + .endObject(); |
| 53 | + assertAcked(prepareCreate("old_index").addMapping("_doc", oldIndexMapping)); |
| 54 | + |
| 55 | + XContentBuilder newIndexMapping = XContentFactory.jsonBuilder() |
| 56 | + .startObject() |
| 57 | + .startObject("_doc") |
| 58 | + .startObject("properties") |
| 59 | + .startObject("distance") |
| 60 | + .field("type", "text") |
| 61 | + .endObject() |
| 62 | + .startObject("route_length_miles") |
| 63 | + .field("type", "double") |
| 64 | + .endObject() |
| 65 | + .endObject() |
| 66 | + .endObject() |
| 67 | + .endObject(); |
| 68 | + assertAcked(prepareCreate("new_index").addMapping("_doc", newIndexMapping)); |
| 69 | + } |
| 70 | + |
| 71 | + public void testFieldAlias() { |
| 72 | + FieldCapabilitiesResponse response = client().prepareFieldCaps().setFields("distance", "route_length_miles") |
| 73 | + .execute().actionGet(); |
| 74 | + |
| 75 | + // Ensure the response has entries for both requested fields. |
| 76 | + assertTrue(response.get().containsKey("distance")); |
| 77 | + assertTrue(response.get().containsKey("route_length_miles")); |
| 78 | + |
| 79 | + // Check the capabilities for the 'distance' field. |
| 80 | + Map<String, FieldCapabilities> distance = response.getField("distance"); |
| 81 | + assertEquals(2, distance.size()); |
| 82 | + |
| 83 | + assertTrue(distance.containsKey("double")); |
| 84 | + assertEquals( |
| 85 | + new FieldCapabilities("distance", "double", true, true, new String[] {"old_index"}, null, null), |
| 86 | + distance.get("double")); |
| 87 | + |
| 88 | + assertTrue(distance.containsKey("text")); |
| 89 | + assertEquals( |
| 90 | + new FieldCapabilities("distance", "text", true, false, new String[] {"new_index"}, null, null), |
| 91 | + distance.get("text")); |
| 92 | + |
| 93 | + // Check the capabilities for the 'route_length_miles' alias. |
| 94 | + Map<String, FieldCapabilities> routeLength = response.getField("route_length_miles"); |
| 95 | + assertEquals(1, routeLength.size()); |
| 96 | + |
| 97 | + assertTrue(routeLength.containsKey("double")); |
| 98 | + assertEquals( |
| 99 | + new FieldCapabilities("route_length_miles", "double", true, true), |
| 100 | + routeLength.get("double")); |
| 101 | + } |
| 102 | + |
| 103 | + public void testFieldAliasWithWildcardField() { |
| 104 | + FieldCapabilitiesResponse response = client().prepareFieldCaps().setFields("route*") |
| 105 | + .execute().actionGet(); |
| 106 | + |
| 107 | + assertEquals(1, response.get().size()); |
| 108 | + assertTrue(response.get().containsKey("route_length_miles")); |
| 109 | + } |
| 110 | +} |
0 commit comments