|
20 | 20 | import org.elasticsearch.cluster.metadata.MappingMetadata;
|
21 | 21 | import org.elasticsearch.cluster.service.ClusterService;
|
22 | 22 | import org.elasticsearch.common.Randomness;
|
| 23 | +import org.elasticsearch.common.Strings; |
23 | 24 | import org.elasticsearch.common.geo.GeoPoint;
|
24 | 25 | import org.elasticsearch.common.settings.Settings;
|
25 |
| -import org.elasticsearch.core.TimeValue; |
26 | 26 | import org.elasticsearch.common.xcontent.XContentBuilder;
|
27 | 27 | import org.elasticsearch.common.xcontent.XContentFactory;
|
| 28 | +import org.elasticsearch.common.xcontent.XContentType; |
| 29 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 30 | +import org.elasticsearch.core.TimeValue; |
28 | 31 | import org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder;
|
29 | 32 | import org.elasticsearch.plugins.Plugin;
|
| 33 | +import org.elasticsearch.rest.RestStatus; |
30 | 34 | import org.elasticsearch.test.ESIntegTestCase;
|
31 | 35 | import org.elasticsearch.test.InternalSettingsPlugin;
|
32 | 36 | import org.hamcrest.Matchers;
|
|
44 | 48 | import static org.elasticsearch.index.mapper.MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING;
|
45 | 49 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
46 | 50 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
|
| 51 | +import static org.hamcrest.Matchers.contains; |
| 52 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
47 | 53 | import static org.hamcrest.Matchers.containsString;
|
48 | 54 | import static org.hamcrest.Matchers.equalTo;
|
49 | 55 | import static org.hamcrest.Matchers.instanceOf;
|
@@ -327,4 +333,78 @@ public void testBulkRequestWithNotFoundDynamicTemplate() throws Exception {
|
327 | 333 | assertThat(bulkItemResponses.getItems()[1].getFailureMessage(),
|
328 | 334 | containsString("Can't find dynamic template for dynamic template name [bar_foo] of field [address.location]"));
|
329 | 335 | }
|
| 336 | + |
| 337 | + public void testDynamicRuntimeNoConflicts() { |
| 338 | + assertAcked(client().admin().indices().prepareCreate("test").setMapping("{\"_doc\":{\"dynamic\":\"runtime\"}}").get()); |
| 339 | + |
| 340 | + List<IndexRequest> docs = new ArrayList<>(); |
| 341 | + docs.add(new IndexRequest("test").source("one.two.three", new int[]{1, 2, 3})); |
| 342 | + docs.add(new IndexRequest("test").source("one.two", 1.2)); |
| 343 | + docs.add(new IndexRequest("test").source("one", "one")); |
| 344 | + docs.add(new IndexRequest("test").source("{\"one\":{\"two\": { \"three\": \"three\"}}}", XContentType.JSON)); |
| 345 | + Collections.shuffle(docs, random()); |
| 346 | + BulkRequest bulkRequest = new BulkRequest(); |
| 347 | + for (IndexRequest doc : docs) { |
| 348 | + bulkRequest.add(doc); |
| 349 | + } |
| 350 | + BulkResponse bulkItemResponses = client().bulk(bulkRequest).actionGet(); |
| 351 | + assertFalse(bulkItemResponses.buildFailureMessage(), bulkItemResponses.hasFailures()); |
| 352 | + |
| 353 | + GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").get(); |
| 354 | + Map<String, Object> sourceAsMap = getMappingsResponse.getMappings().get("test").sourceAsMap(); |
| 355 | + assertFalse(sourceAsMap.containsKey("properties")); |
| 356 | + @SuppressWarnings("unchecked") |
| 357 | + Map<String, Object> runtime = (Map<String, Object>)sourceAsMap.get("runtime"); |
| 358 | + //depending on the order of the documents field types may differ, but there are no mapping conflicts |
| 359 | + assertThat(runtime.keySet(), containsInAnyOrder("one", "one.two", "one.two.three")); |
| 360 | + } |
| 361 | + |
| 362 | + public void testDynamicRuntimeObjectFields() { |
| 363 | + assertAcked(client().admin().indices().prepareCreate("test").setMapping("{\"_doc\":{\"properties\":{" + |
| 364 | + "\"obj\":{\"properties\":{\"runtime\":{\"type\":\"object\",\"dynamic\":\"runtime\"}}}}}}").get()); |
| 365 | + |
| 366 | + List<IndexRequest> docs = new ArrayList<>(); |
| 367 | + docs.add(new IndexRequest("test").source("obj.one", 1)); |
| 368 | + docs.add(new IndexRequest("test").source("anything", 1)); |
| 369 | + docs.add(new IndexRequest("test").source("obj.runtime.one.two", "test")); |
| 370 | + docs.add(new IndexRequest("test").source("obj.runtime.one", "one")); |
| 371 | + docs.add(new IndexRequest("test").source("{\"obj\":{\"runtime\":{\"one\":{\"two\": \"test\"}}}}", XContentType.JSON)); |
| 372 | + Collections.shuffle(docs, random()); |
| 373 | + BulkRequest bulkRequest = new BulkRequest(); |
| 374 | + for (IndexRequest doc : docs) { |
| 375 | + bulkRequest.add(doc); |
| 376 | + } |
| 377 | + BulkResponse bulkItemResponses = client().bulk(bulkRequest).actionGet(); |
| 378 | + assertFalse(bulkItemResponses.buildFailureMessage(), bulkItemResponses.hasFailures()); |
| 379 | + |
| 380 | + MapperParsingException exception = expectThrows(MapperParsingException.class, |
| 381 | + () -> client().prepareIndex("test").setSource("obj.runtime", "value").get()); |
| 382 | + assertEquals("object mapping for [obj.runtime] tried to parse field [obj.runtime] as object, but found a concrete value", |
| 383 | + exception.getMessage()); |
| 384 | + |
| 385 | + assertEquals("{\"test\":{\"mappings\":" + |
| 386 | + "{\"runtime\":{\"obj.runtime.one\":{\"type\":\"keyword\"},\"obj.runtime.one.two\":{\"type\":\"keyword\"}}," + |
| 387 | + "\"properties\":{\"anything\":{\"type\":\"long\"}," + |
| 388 | + "\"obj\":{\"properties\":{\"one\":{\"type\":\"long\"}," + |
| 389 | + "\"runtime\":{\"type\":\"object\",\"dynamic\":\"runtime\"}}}}}}}", |
| 390 | + Strings.toString(client().admin().indices().prepareGetMappings("test").get())); |
| 391 | + |
| 392 | + assertAcked(client().admin().indices().preparePutMapping("test").setSource("{\"_doc\":{\"properties\":{\"obj\":{\"properties\":" + |
| 393 | + "{\"runtime\":{\"properties\":{\"dynamic\":{\"type\":\"object\", \"dynamic\":true}}}}}}}}", XContentType.JSON)); |
| 394 | + |
| 395 | + assertEquals(RestStatus.CREATED, client().prepareIndex("test").setSource("obj.runtime.dynamic.leaf", 1).get().status()); |
| 396 | + GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").get(); |
| 397 | + Map<String, Object> sourceAsMap = getMappingsResponse.getMappings().get("test").sourceAsMap(); |
| 398 | + assertThat( |
| 399 | + XContentMapValues.extractRawValues("properties.obj.properties.runtime.properties.dynamic.properties.leaf.type", sourceAsMap), |
| 400 | + contains("long")); |
| 401 | + } |
| 402 | + |
| 403 | + private static Map<String, Object> getMappedField(Map<String, Object> sourceAsMap, String name) { |
| 404 | + @SuppressWarnings("unchecked") |
| 405 | + Map<String, Object> properties = (Map<String, Object>)sourceAsMap.get("properties"); |
| 406 | + @SuppressWarnings("unchecked") |
| 407 | + Map<String, Object> mappedField = (Map<String, Object>)properties.get(name); |
| 408 | + return mappedField; |
| 409 | + } |
330 | 410 | }
|
0 commit comments