|
19 | 19 |
|
20 | 20 | package org.elasticsearch.index.mapper;
|
21 | 21 |
|
22 |
| -import java.util.HashMap; |
23 | 22 | import java.util.HashSet;
|
24 | 23 | import org.apache.lucene.index.IndexableField;
|
25 | 24 | import org.elasticsearch.Version;
|
26 | 25 | import org.elasticsearch.common.compress.CompressedXContent;
|
27 | 26 | import org.elasticsearch.common.settings.Settings;
|
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
28 | 28 | import org.elasticsearch.common.xcontent.XContentFactory;
|
29 | 29 | import org.elasticsearch.common.xcontent.XContentType;
|
30 | 30 | import org.elasticsearch.index.mapper.MapperService.MergeReason;
|
@@ -524,4 +524,144 @@ public void testParentObjectMapperAreNested() throws Exception {
|
524 | 524 | assertFalse(objectMapper.parentObjectMapperAreNested(mapperService));
|
525 | 525 | }
|
526 | 526 |
|
| 527 | + public void testLimitNestedDocsDefaultSettings() throws Exception{ |
| 528 | + Settings settings = Settings.builder().build(); |
| 529 | + MapperService mapperService = createIndex("test1", settings).mapperService(); |
| 530 | + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") |
| 531 | + .startObject("nested1").field("type", "nested").endObject() |
| 532 | + .endObject().endObject().endObject().string(); |
| 533 | + DocumentMapper docMapper = mapperService.documentMapperParser().parse("type", new CompressedXContent(mapping)); |
| 534 | + long defaultMaxNoNestedDocs = MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.get(settings); |
| 535 | + |
| 536 | + // parsing a doc with No. nested objects > defaultMaxNoNestedDocs fails |
| 537 | + XContentBuilder docBuilder = XContentFactory.jsonBuilder(); |
| 538 | + docBuilder.startObject(); |
| 539 | + { |
| 540 | + docBuilder.startArray("nested1"); |
| 541 | + { |
| 542 | + for(int i = 0; i <= defaultMaxNoNestedDocs; i++) { |
| 543 | + docBuilder.startObject().field("f", i).endObject(); |
| 544 | + } |
| 545 | + } |
| 546 | + docBuilder.endArray(); |
| 547 | + } |
| 548 | + docBuilder.endObject(); |
| 549 | + SourceToParse source1 = SourceToParse.source("test1", "type", "1", docBuilder.bytes(), XContentType.JSON); |
| 550 | + MapperParsingException e = expectThrows(MapperParsingException.class, () -> docMapper.parse(source1)); |
| 551 | + assertEquals( |
| 552 | + "The number of nested documents has exceeded the allowed limit of [" + defaultMaxNoNestedDocs |
| 553 | + + "]. This limit can be set by changing the [" + MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey() |
| 554 | + + "] index level setting.", |
| 555 | + e.getMessage() |
| 556 | + ); |
| 557 | + } |
| 558 | + |
| 559 | + public void testLimitNestedDocs() throws Exception{ |
| 560 | + // setting limit to allow only two nested objects in the whole doc |
| 561 | + long maxNoNestedDocs = 2L; |
| 562 | + MapperService mapperService = createIndex("test1", Settings.builder() |
| 563 | + .put(MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey(), maxNoNestedDocs).build()).mapperService(); |
| 564 | + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") |
| 565 | + .startObject("nested1").field("type", "nested").endObject() |
| 566 | + .endObject().endObject().endObject().string(); |
| 567 | + DocumentMapper docMapper = mapperService.documentMapperParser().parse("type", new CompressedXContent(mapping)); |
| 568 | + |
| 569 | + // parsing a doc with 2 nested objects succeeds |
| 570 | + XContentBuilder docBuilder = XContentFactory.jsonBuilder(); |
| 571 | + docBuilder.startObject(); |
| 572 | + { |
| 573 | + docBuilder.startArray("nested1"); |
| 574 | + { |
| 575 | + docBuilder.startObject().field("field1", "11").field("field2", "21").endObject(); |
| 576 | + docBuilder.startObject().field("field1", "12").field("field2", "22").endObject(); |
| 577 | + } |
| 578 | + docBuilder.endArray(); |
| 579 | + } |
| 580 | + docBuilder.endObject(); |
| 581 | + SourceToParse source1 = SourceToParse.source("test1", "type", "1", docBuilder.bytes(), XContentType.JSON); |
| 582 | + ParsedDocument doc = docMapper.parse(source1); |
| 583 | + assertThat(doc.docs().size(), equalTo(3)); |
| 584 | + |
| 585 | + // parsing a doc with 3 nested objects fails |
| 586 | + XContentBuilder docBuilder2 = XContentFactory.jsonBuilder(); |
| 587 | + docBuilder2.startObject(); |
| 588 | + { |
| 589 | + docBuilder2.startArray("nested1"); |
| 590 | + { |
| 591 | + docBuilder2.startObject().field("field1", "11").field("field2", "21").endObject(); |
| 592 | + docBuilder2.startObject().field("field1", "12").field("field2", "22").endObject(); |
| 593 | + docBuilder2.startObject().field("field1", "13").field("field2", "23").endObject(); |
| 594 | + } |
| 595 | + docBuilder2.endArray(); |
| 596 | + } |
| 597 | + docBuilder2.endObject(); |
| 598 | + SourceToParse source2 = SourceToParse.source("test1", "type", "2", docBuilder2.bytes(), XContentType.JSON); |
| 599 | + MapperParsingException e = expectThrows(MapperParsingException.class, () -> docMapper.parse(source2)); |
| 600 | + assertEquals( |
| 601 | + "The number of nested documents has exceeded the allowed limit of [" + maxNoNestedDocs |
| 602 | + + "]. This limit can be set by changing the [" + MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey() |
| 603 | + + "] index level setting.", |
| 604 | + e.getMessage() |
| 605 | + ); |
| 606 | + } |
| 607 | + |
| 608 | + public void testLimitNestedDocsMultipleNestedFields() throws Exception{ |
| 609 | + // setting limit to allow only two nested objects in the whole doc |
| 610 | + long maxNoNestedDocs = 2L; |
| 611 | + MapperService mapperService = createIndex("test1", Settings.builder() |
| 612 | + .put(MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey(), maxNoNestedDocs).build()).mapperService(); |
| 613 | + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") |
| 614 | + .startObject("nested1").field("type", "nested").endObject() |
| 615 | + .startObject("nested2").field("type", "nested").endObject() |
| 616 | + .endObject().endObject().endObject().string(); |
| 617 | + DocumentMapper docMapper = mapperService.documentMapperParser().parse("type", new CompressedXContent(mapping)); |
| 618 | + |
| 619 | + // parsing a doc with 2 nested objects succeeds |
| 620 | + XContentBuilder docBuilder = XContentFactory.jsonBuilder(); |
| 621 | + docBuilder.startObject(); |
| 622 | + { |
| 623 | + docBuilder.startArray("nested1"); |
| 624 | + { |
| 625 | + docBuilder.startObject().field("field1", "11").field("field2", "21").endObject(); |
| 626 | + } |
| 627 | + docBuilder.endArray(); |
| 628 | + docBuilder.startArray("nested2"); |
| 629 | + { |
| 630 | + docBuilder.startObject().field("field1", "21").field("field2", "22").endObject(); |
| 631 | + } |
| 632 | + docBuilder.endArray(); |
| 633 | + } |
| 634 | + docBuilder.endObject(); |
| 635 | + SourceToParse source1 = SourceToParse.source("test1", "type", "1", docBuilder.bytes(), XContentType.JSON); |
| 636 | + ParsedDocument doc = docMapper.parse(source1); |
| 637 | + assertThat(doc.docs().size(), equalTo(3)); |
| 638 | + |
| 639 | + // parsing a doc with 3 nested objects fails |
| 640 | + XContentBuilder docBuilder2 = XContentFactory.jsonBuilder(); |
| 641 | + docBuilder2.startObject(); |
| 642 | + { |
| 643 | + docBuilder2.startArray("nested1"); |
| 644 | + { |
| 645 | + docBuilder2.startObject().field("field1", "11").field("field2", "21").endObject(); |
| 646 | + } |
| 647 | + docBuilder2.endArray(); |
| 648 | + docBuilder2.startArray("nested2"); |
| 649 | + { |
| 650 | + docBuilder2.startObject().field("field1", "12").field("field2", "22").endObject(); |
| 651 | + docBuilder2.startObject().field("field1", "13").field("field2", "23").endObject(); |
| 652 | + } |
| 653 | + docBuilder2.endArray(); |
| 654 | + |
| 655 | + } |
| 656 | + docBuilder2.endObject(); |
| 657 | + SourceToParse source2 = SourceToParse.source("test1", "type", "2", docBuilder2.bytes(), XContentType.JSON); |
| 658 | + MapperParsingException e = expectThrows(MapperParsingException.class, () -> docMapper.parse(source2)); |
| 659 | + assertEquals( |
| 660 | + "The number of nested documents has exceeded the allowed limit of [" + maxNoNestedDocs |
| 661 | + + "]. This limit can be set by changing the [" + MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey() |
| 662 | + + "] index level setting.", |
| 663 | + e.getMessage() |
| 664 | + ); |
| 665 | + } |
| 666 | + |
527 | 667 | }
|
0 commit comments