-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Don't return a parent field if its type is not listed in the types parameter #123148
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
5a320fc
c89aceb
f252a5c
91afc7f
f1f1496
4dc545d
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 |
|---|---|---|
|
|
@@ -249,6 +249,111 @@ public void testFieldTypeFiltering() throws IOException { | |
| assertNull(response.get("_index")); | ||
| } | ||
|
|
||
| public void testExcludeParentWhenFieldTypeFilteringDoNotIncludeItsType() throws IOException { | ||
| MapperService mapperService = createMapperService(""" | ||
| { "_doc" : { | ||
| "properties" : { | ||
| "field1" : { "type" : "keyword" }, | ||
| "field2" : { "type" : "long" }, | ||
| "field3" : { "type" : "text" }, | ||
| "field4" : { | ||
| "type" : "object", | ||
| "properties" : { | ||
| "field5" : { "type" : "keyword" } | ||
| } | ||
| } | ||
| } | ||
| } } | ||
| """); | ||
| SearchExecutionContext sec = createSearchExecutionContext(mapperService); | ||
|
|
||
| Map<String, IndexFieldCapabilities> response = FieldCapabilitiesFetcher.retrieveFieldCaps( | ||
| sec, | ||
| s -> true, | ||
| Strings.EMPTY_ARRAY, | ||
| new String[] { "text", "keyword" }, | ||
| FieldPredicate.ACCEPT_ALL, | ||
| getMockIndexShard(), | ||
| true | ||
| ); | ||
| assertNotNull(response.get("field1")); | ||
| assertNull(response.get("field2")); | ||
| assertNotNull(response.get("field3")); | ||
| assertNull(response.get("field4")); | ||
| assertNotNull(response.get("field4.field5")); | ||
| assertNull(response.get("_index")); | ||
| } | ||
|
|
||
| public void testIncludeParentWhenFieldTypeFilteringIncludeItsType() throws IOException { | ||
| MapperService mapperService = createMapperService(""" | ||
| { "_doc" : { | ||
| "properties" : { | ||
| "field1" : { "type" : "keyword" }, | ||
| "field2" : { "type" : "long" }, | ||
| "field3" : { "type" : "text" }, | ||
| "field4" : { | ||
| "type" : "object", | ||
| "properties" : { | ||
| "field5" : { "type" : "keyword" } | ||
| } | ||
| } | ||
| } | ||
| } } | ||
| """); | ||
| SearchExecutionContext sec = createSearchExecutionContext(mapperService); | ||
|
|
||
| Map<String, IndexFieldCapabilities> response = FieldCapabilitiesFetcher.retrieveFieldCaps( | ||
| sec, | ||
| s -> true, | ||
| Strings.EMPTY_ARRAY, | ||
| new String[] { "text", "keyword", "object" }, | ||
| FieldPredicate.ACCEPT_ALL, | ||
| getMockIndexShard(), | ||
| true | ||
| ); | ||
| assertNotNull(response.get("field1")); | ||
| assertNull(response.get("field2")); | ||
| assertNotNull(response.get("field3")); | ||
| assertNotNull(response.get("field4")); | ||
| assertNotNull(response.get("field4.field5")); | ||
| assertNull(response.get("_index")); | ||
| } | ||
|
|
||
| public void testIncludeAllFieldsWhenNoFieldTypeFiltering() throws IOException { | ||
| MapperService mapperService = createMapperService(""" | ||
| { "_doc" : { | ||
| "properties" : { | ||
| "field1" : { "type" : "keyword" }, | ||
| "field2" : { "type" : "long" }, | ||
| "field3" : { "type" : "text" }, | ||
| "field4" : { | ||
| "type" : "object", | ||
| "properties" : { | ||
| "field5" : { "type" : "keyword" } | ||
| } | ||
| } | ||
| } | ||
| } } | ||
| """); | ||
| SearchExecutionContext sec = createSearchExecutionContext(mapperService); | ||
|
|
||
| Map<String, IndexFieldCapabilities> response = FieldCapabilitiesFetcher.retrieveFieldCaps( | ||
| sec, | ||
| s -> true, | ||
| Strings.EMPTY_ARRAY, | ||
| Strings.EMPTY_ARRAY, | ||
| FieldPredicate.ACCEPT_ALL, | ||
| getMockIndexShard(), | ||
| true | ||
| ); | ||
| assertNotNull(response.get("field1")); | ||
| assertNotNull(response.get("field2")); | ||
| assertNotNull(response.get("field3")); | ||
| assertNotNull(response.get("field4")); | ||
| assertNotNull(response.get("field4.field5")); | ||
| assertNotNull(response.get("_index")); | ||
| } | ||
|
|
||
|
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. How should this work if we use the filters
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. I added two extra tests to cover that. What surprised me is that |
||
| private IndexShard getMockIndexShard() { | ||
| IndexShard indexShard = mock(IndexShard.class); | ||
| when(indexShard.getFieldInfos()).thenReturn(FieldInfos.EMPTY); | ||
|
|
||
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.
Careful, by calling
Arrays.asListin a loop, here we are converting every time the array to a newArrayListand it is not very efficient.There are a couple of options here:
Side note: I don't expect this array to ever become huge therefore both approach should be safe to use but to be extra safe I'd opt for the Set.
If we are using Set we can even go for
Set.of(types)since it returns an unmodifiable set!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.
Yep, that makes sense. To avoid the duplicated initialization of
Set.of(types), I changed some other places. Also, now I have to check for theparentin the filters, so I used the sameSetstrategy.