-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Limit the number of nested documents #27405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -525,35 +525,42 @@ public void testParentObjectMapperAreNested() throws Exception { | |
| } | ||
|
|
||
| public void testLimitNestedDocsDefaultSettings() throws Exception{ | ||
| MapperService mapperService = createIndex("test1", Settings.builder().build()).mapperService(); | ||
| Settings settings = Settings.builder().build(); | ||
| MapperService mapperService = createIndex("test1", settings).mapperService(); | ||
| String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") | ||
| .startObject("nested1").field("type", "nested").endObject() | ||
| .endObject().endObject().endObject().string(); | ||
| DocumentMapper docMapper = mapperService.documentMapperParser().parse("type", new CompressedXContent(mapping)); | ||
| long defaultMaxNoNestedDocs = MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.get(settings); | ||
|
|
||
| // parsing a doc with 3 nested objects succeeds | ||
| // parsing a doc with No. nested objects > defaultMaxNoNestedDocs fails | ||
| XContentBuilder docBuilder = XContentFactory.jsonBuilder(); | ||
| docBuilder.startObject(); | ||
| { | ||
| docBuilder.startArray("nested1"); | ||
| { | ||
| docBuilder.startObject().field("field1", "11").field("field2", "21").endObject(); | ||
| docBuilder.startObject().field("field1", "12").field("field2", "22").endObject(); | ||
| docBuilder.startObject().field("field1", "13").field("field2", "23").endObject(); | ||
| for(int i=0; i<=defaultMaxNoNestedDocs; i++) { | ||
| docBuilder.startObject().field("f", i).endObject(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm suprised this triggers the no. fields warnings since "f" is not the field thats defined with the "nested" type above? I would have expected this to not trigger the limit but the exception tested below seems to indicate it triggers the setting. What am I missing?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbuescher Please correct me if I misunderstood your previous comment: "could you change this to that the the document has more than the default number of nested docs (simple loop) and gets rejected".
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I probably read the test to quickly and missed the startArray() part. You are absolutely right, this is how I expected the test. Sorry for the noise. |
||
| } | ||
| } | ||
| docBuilder.endArray(); | ||
| } | ||
| docBuilder.endObject(); | ||
| SourceToParse source1 = SourceToParse.source("test1", "type", "1", docBuilder.bytes(), XContentType.JSON); | ||
| ParsedDocument doc = docMapper.parse(source1); | ||
| assertThat(doc.docs().size(), equalTo(4)); | ||
| MapperParsingException e = expectThrows(MapperParsingException.class, () -> docMapper.parse(source1)); | ||
| assertEquals( | ||
| "The number of nested documents has exceeded the allowed limit of [" + defaultMaxNoNestedDocs | ||
| + "]. This limit can be set by changing the [" + MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey() | ||
| + "] index level setting.", | ||
| e.getMessage() | ||
| ); | ||
| } | ||
|
|
||
| public void testLimitNestedDocs() throws Exception{ | ||
| // setting limit to allow only two nested objects in the whole doc | ||
| long nested_docs_limit = 2L; | ||
| long maxNoNestedDocs = 2L; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just as feedback, nothing to change really, but I liked the previous variable name better ;-)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbuescher thanks for the feedback, I thought in the previous variable names I did not follow Java camel case standards for variable names (was more of python style)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, and you are right, camel case is preferred. I probably misread the "NoNestedDocs" part of the name as "no nested docs" and that confused me for a second, but either way is fine.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cbuescher thanks Christoph, you are right |
||
| MapperService mapperService = createIndex("test1", Settings.builder() | ||
| .put(MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey(), nested_docs_limit).build()).mapperService(); | ||
| .put(MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey(), maxNoNestedDocs).build()).mapperService(); | ||
| String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") | ||
| .startObject("nested1").field("type", "nested").endObject() | ||
| .endObject().endObject().endObject().string(); | ||
|
|
@@ -591,7 +598,7 @@ public void testLimitNestedDocs() throws Exception{ | |
| SourceToParse source2 = SourceToParse.source("test1", "type", "2", docBuilder2.bytes(), XContentType.JSON); | ||
| MapperParsingException e = expectThrows(MapperParsingException.class, () -> docMapper.parse(source2)); | ||
| assertEquals( | ||
| "The number of nested documents has exceeded the allowed limit of [" + nested_docs_limit | ||
| "The number of nested documents has exceeded the allowed limit of [" + maxNoNestedDocs | ||
| + "]. This limit can be set by changing the [" + MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey() | ||
| + "] index level setting.", | ||
| e.getMessage() | ||
|
|
@@ -600,9 +607,9 @@ public void testLimitNestedDocs() throws Exception{ | |
|
|
||
| public void testLimitNestedDocsMultipleNestedFields() throws Exception{ | ||
|
||
| // setting limit to allow only two nested objects in the whole doc | ||
| long nested_docs_limit = 2L; | ||
| long maxNoNestedDocs = 2L; | ||
| MapperService mapperService = createIndex("test1", Settings.builder() | ||
| .put(MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey(), nested_docs_limit).build()).mapperService(); | ||
| .put(MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey(), maxNoNestedDocs).build()).mapperService(); | ||
| String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") | ||
| .startObject("nested1").field("type", "nested").endObject() | ||
| .startObject("nested2").field("type", "nested").endObject() | ||
|
|
@@ -650,7 +657,7 @@ public void testLimitNestedDocsMultipleNestedFields() throws Exception{ | |
| SourceToParse source2 = SourceToParse.source("test1", "type", "2", docBuilder2.bytes(), XContentType.JSON); | ||
| MapperParsingException e = expectThrows(MapperParsingException.class, () -> docMapper.parse(source2)); | ||
| assertEquals( | ||
| "The number of nested documents has exceeded the allowed limit of [" + nested_docs_limit | ||
| "The number of nested documents has exceeded the allowed limit of [" + maxNoNestedDocs | ||
| + "]. This limit can be set by changing the [" + MapperService.INDEX_MAPPING_NESTED_DOCS_LIMIT_SETTING.getKey() | ||
| + "] index level setting.", | ||
| e.getMessage() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: some whitespaces around the operators etc... would be nice