|
42 | 42 | import static org.hamcrest.Matchers.containsString;
|
43 | 43 | import static org.hamcrest.Matchers.equalTo;
|
44 | 44 | import static org.hamcrest.Matchers.instanceOf;
|
| 45 | +import static org.hamcrest.Matchers.not; |
45 | 46 |
|
46 | 47 | public class GeoShapeFieldMapperTests extends ESSingleNodeTestCase {
|
47 | 48 |
|
@@ -588,10 +589,65 @@ public void testSerializeDefaults() throws Exception {
|
588 | 589 | }
|
589 | 590 | }
|
590 | 591 |
|
591 |
| - public String toXContentString(GeoShapeFieldMapper mapper) throws IOException { |
| 592 | + public void testPointsOnlyDefaultsWithTermStrategy() throws IOException { |
| 593 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 594 | + .startObject("properties").startObject("location") |
| 595 | + .field("type", "geo_shape") |
| 596 | + .field("tree", "quadtree") |
| 597 | + .field("precision", "10m") |
| 598 | + .field("strategy", "term") |
| 599 | + .endObject().endObject() |
| 600 | + .endObject().endObject()); |
| 601 | + |
| 602 | + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping)); |
| 603 | + FieldMapper fieldMapper = defaultMapper.mappers().getMapper("location"); |
| 604 | + assertThat(fieldMapper, instanceOf(GeoShapeFieldMapper.class)); |
| 605 | + |
| 606 | + GeoShapeFieldMapper geoShapeFieldMapper = (GeoShapeFieldMapper) fieldMapper; |
| 607 | + PrefixTreeStrategy strategy = geoShapeFieldMapper.fieldType().defaultStrategy(); |
| 608 | + |
| 609 | + assertThat(strategy.getDistErrPct(), equalTo(0.0)); |
| 610 | + assertThat(strategy.getGrid(), instanceOf(QuadPrefixTree.class)); |
| 611 | + assertThat(strategy.getGrid().getMaxLevels(), equalTo(23)); |
| 612 | + assertThat(strategy.isPointsOnly(), equalTo(true)); |
| 613 | + // term strategy changes the default for points_only, check that we handle it correctly |
| 614 | + assertThat(toXContentString(geoShapeFieldMapper, false), not(containsString("points_only"))); |
| 615 | + } |
| 616 | + |
| 617 | + |
| 618 | + public void testPointsOnlyFalseWithTermStrategy() throws Exception { |
| 619 | + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") |
| 620 | + .startObject("properties").startObject("location") |
| 621 | + .field("type", "geo_shape") |
| 622 | + .field("tree", "quadtree") |
| 623 | + .field("precision", "10m") |
| 624 | + .field("strategy", "term") |
| 625 | + .field("points_only", false) |
| 626 | + .endObject().endObject() |
| 627 | + .endObject().endObject()); |
| 628 | + |
| 629 | + DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser(); |
| 630 | + |
| 631 | + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, |
| 632 | + () -> parser.parse("type1", new CompressedXContent(mapping)) |
| 633 | + ); |
| 634 | + assertThat(e.getMessage(), containsString("points_only cannot be set to false for term strategy")); |
| 635 | + } |
| 636 | + |
| 637 | + public String toXContentString(GeoShapeFieldMapper mapper, boolean includeDefaults) throws IOException { |
592 | 638 | XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
|
593 |
| - mapper.doXContentBody(builder, true, new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true"))); |
| 639 | + ToXContent.Params params; |
| 640 | + if (includeDefaults) { |
| 641 | + params = new ToXContent.MapParams(Collections.singletonMap("include_defaults", "true")); |
| 642 | + } else { |
| 643 | + params = ToXContent.EMPTY_PARAMS; |
| 644 | + } |
| 645 | + mapper.doXContentBody(builder, includeDefaults, params); |
594 | 646 | return Strings.toString(builder.endObject());
|
595 | 647 | }
|
596 | 648 |
|
| 649 | + public String toXContentString(GeoShapeFieldMapper mapper) throws IOException { |
| 650 | + return toXContentString(mapper, true); |
| 651 | + } |
| 652 | + |
597 | 653 | }
|
0 commit comments