|
27 | 27 | import org.elasticsearch.common.compress.CompressedXContent; |
28 | 28 | import org.elasticsearch.common.geo.GeoUtils; |
29 | 29 | import org.elasticsearch.common.geo.builders.ShapeBuilder; |
| 30 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 31 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
30 | 32 | import org.elasticsearch.common.xcontent.XContentFactory; |
31 | 33 | import org.elasticsearch.plugins.Plugin; |
32 | 34 | import org.elasticsearch.test.ESSingleNodeTestCase; |
33 | 35 | import org.elasticsearch.test.InternalSettingsPlugin; |
34 | 36 |
|
35 | 37 | import java.io.IOException; |
36 | 38 | import java.util.Collection; |
| 39 | +import java.util.Collections; |
37 | 40 |
|
38 | 41 | import static org.elasticsearch.index.mapper.GeoPointFieldMapper.Names.IGNORE_Z_VALUE; |
39 | 42 | import static org.hamcrest.Matchers.containsString; |
@@ -517,4 +520,78 @@ public void testEmptyName() throws Exception { |
517 | 520 | assertThat(e.getMessage(), containsString("name cannot be empty string")); |
518 | 521 | } |
519 | 522 |
|
| 523 | + public void testSerializeDefaults() throws Exception { |
| 524 | + DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); |
| 525 | + { |
| 526 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 527 | + .startObject("properties").startObject("location") |
| 528 | + .field("type", "geo_shape") |
| 529 | + .field("tree", "quadtree") |
| 530 | + .endObject().endObject() |
| 531 | + .endObject().endObject()); |
| 532 | + DocumentMapper defaultMapper = parser.parse("type1", new CompressedXContent(mapping)); |
| 533 | + String serialized = toXContentString((GeoShapeFieldMapper) defaultMapper.mappers().getMapper("location")); |
| 534 | + assertTrue(serialized, serialized.contains("\"precision\":\"50.0m\"")); |
| 535 | + assertTrue(serialized, serialized.contains("\"tree_levels\":21")); |
| 536 | + } |
| 537 | + { |
| 538 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 539 | + .startObject("properties").startObject("location") |
| 540 | + .field("type", "geo_shape") |
| 541 | + .field("tree", "geohash") |
| 542 | + .endObject().endObject() |
| 543 | + .endObject().endObject()); |
| 544 | + DocumentMapper defaultMapper = parser.parse("type1", new CompressedXContent(mapping)); |
| 545 | + String serialized = toXContentString((GeoShapeFieldMapper) defaultMapper.mappers().getMapper("location")); |
| 546 | + assertTrue(serialized, serialized.contains("\"precision\":\"50.0m\"")); |
| 547 | + assertTrue(serialized, serialized.contains("\"tree_levels\":9")); |
| 548 | + } |
| 549 | + { |
| 550 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 551 | + .startObject("properties").startObject("location") |
| 552 | + .field("type", "geo_shape") |
| 553 | + .field("tree", "quadtree") |
| 554 | + .field("tree_levels", "6") |
| 555 | + .endObject().endObject() |
| 556 | + .endObject().endObject()); |
| 557 | + DocumentMapper defaultMapper = parser.parse("type1", new CompressedXContent(mapping)); |
| 558 | + String serialized = toXContentString((GeoShapeFieldMapper) defaultMapper.mappers().getMapper("location")); |
| 559 | + assertFalse(serialized, serialized.contains("\"precision\":")); |
| 560 | + assertTrue(serialized, serialized.contains("\"tree_levels\":6")); |
| 561 | + } |
| 562 | + { |
| 563 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 564 | + .startObject("properties").startObject("location") |
| 565 | + .field("type", "geo_shape") |
| 566 | + .field("tree", "quadtree") |
| 567 | + .field("precision", "6") |
| 568 | + .endObject().endObject() |
| 569 | + .endObject().endObject()); |
| 570 | + DocumentMapper defaultMapper = parser.parse("type1", new CompressedXContent(mapping)); |
| 571 | + String serialized = toXContentString((GeoShapeFieldMapper) defaultMapper.mappers().getMapper("location")); |
| 572 | + assertTrue(serialized, serialized.contains("\"precision\":\"6.0m\"")); |
| 573 | + assertFalse(serialized, serialized.contains("\"tree_levels\":")); |
| 574 | + } |
| 575 | + { |
| 576 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 577 | + .startObject("properties").startObject("location") |
| 578 | + .field("type", "geo_shape") |
| 579 | + .field("tree", "quadtree") |
| 580 | + .field("precision", "6m") |
| 581 | + .field("tree_levels", "5") |
| 582 | + .endObject().endObject() |
| 583 | + .endObject().endObject()); |
| 584 | + DocumentMapper defaultMapper = parser.parse("type1", new CompressedXContent(mapping)); |
| 585 | + String serialized = toXContentString((GeoShapeFieldMapper) defaultMapper.mappers().getMapper("location")); |
| 586 | + assertTrue(serialized, serialized.contains("\"precision\":\"6.0m\"")); |
| 587 | + assertTrue(serialized, serialized.contains("\"tree_levels\":5")); |
| 588 | + } |
| 589 | + } |
| 590 | + |
| 591 | + public String toXContentString(GeoShapeFieldMapper mapper) throws IOException { |
| 592 | + XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); |
| 593 | + mapper.doXContentBody(builder, true, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true"))); |
| 594 | + return Strings.toString(builder.endObject()); |
| 595 | + } |
| 596 | + |
520 | 597 | } |
0 commit comments