Skip to content

Commit

Permalink
Do not filter source if exclude contains * (#108501)
Browse files Browse the repository at this point in the history
This commit prevents the serialization of source if not needed.
  • Loading branch information
iverase committed May 10, 2024
1 parent d2d1357 commit 7ed58e7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Expand Up @@ -28,7 +28,7 @@ public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) {
}
assert fetchSourceContext.fetchSource();
SourceFilter sourceFilter = fetchSourceContext.filter();

final boolean filterExcludesAll = sourceFilter.excludesAll();
return new FetchSubPhaseProcessor() {
private int fastPath;

Expand Down Expand Up @@ -67,8 +67,13 @@ private void hitExecute(FetchSourceContext fetchSourceContext, HitContext hitCon
return;
}

// Otherwise, filter the source and add it to the hit.
source = source.filter(sourceFilter);
if (filterExcludesAll) {
// we can just add an empty map
source = Source.empty(source.sourceContentType());
} else {
// Otherwise, filter the source and add it to the hit.
source = source.filter(sourceFilter);
}
if (nestedHit) {
source = extractNested(source, hitContext.hit().getNestedIdentity());
}
Expand Down
Expand Up @@ -109,4 +109,8 @@ private Function<Source, Source> buildBytesFilter() {
}
};
}

public boolean excludesAll() {
return Arrays.asList(excludes).contains("*");
}
}
Expand Up @@ -52,6 +52,27 @@ public void testBasicFiltering() throws IOException {
assertEquals(Collections.singletonMap("field1", "value"), hitContext.hit().getSourceAsMap());
}

public void testExcludesAll() throws IOException {
XContentBuilder source = XContentFactory.jsonBuilder().startObject().field("field1", "value").field("field2", "value2").endObject();
HitContext hitContext = hitExecute(source, false, null, null);
assertNull(hitContext.hit().getSourceAsMap());

hitContext = hitExecute(source, true, "field1", "*");
assertEquals(Collections.emptyMap(), hitContext.hit().getSourceAsMap());

hitContext = hitExecute(source, true, null, "*");
assertEquals(Collections.emptyMap(), hitContext.hit().getSourceAsMap());

hitContext = hitExecute(source, true, "*", "*");
assertEquals(Collections.emptyMap(), hitContext.hit().getSourceAsMap());

hitContext = hitExecuteMultiple(source, true, new String[] { "field1", "field2" }, new String[] { "*", "field1" });
assertEquals(Collections.emptyMap(), hitContext.hit().getSourceAsMap());

hitContext = hitExecuteMultiple(source, true, null, new String[] { "field2", "*", "field1" });
assertEquals(Collections.emptyMap(), hitContext.hit().getSourceAsMap());
}

public void testMultipleFiltering() throws IOException {
XContentBuilder source = XContentFactory.jsonBuilder().startObject().field("field", "value").field("field2", "value2").endObject();
HitContext hitContext = hitExecuteMultiple(source, true, new String[] { "*.notexisting", "field" }, null);
Expand Down

0 comments on commit 7ed58e7

Please sign in to comment.