This repository was archived by the owner on Apr 23, 2026. It is now read-only.
forked from Signal65/elasticsearch-Copilot
-
Notifications
You must be signed in to change notification settings - Fork 1
Add XContentFieldFilter (#81970) #3
Open
MitchLewis930
wants to merge
1
commit into
pr_013_before
Choose a base branch
from
pr_013_after
base: pr_013_before
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
server/src/main/java/org/elasticsearch/common/xcontent/XContentFieldFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.xcontent; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.BytesStreamOutput; | ||
| import org.elasticsearch.common.util.CollectionUtils; | ||
| import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
| import org.elasticsearch.core.Nullable; | ||
| import org.elasticsearch.core.Tuple; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentFactory; | ||
| import org.elasticsearch.xcontent.XContentParser; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * A filter that filter fields away from source | ||
| */ | ||
| public interface XContentFieldFilter { | ||
| /** | ||
| * filter source in {@link BytesReference} format and in {@link XContentType} content type | ||
| * note that xContentType may be null in some case, we should guess xContentType from sourceBytes in such cases | ||
| */ | ||
| BytesReference apply(BytesReference sourceBytes, @Nullable XContentType xContentType) throws IOException; | ||
|
|
||
| /** | ||
| * Construct {@link XContentFieldFilter} using given includes and excludes | ||
| * | ||
| * @param includes fields to keep, wildcard supported | ||
| * @param excludes fields to remove, wildcard supported | ||
| * @return filter using {@link XContentMapValues#filter(String[], String[])} if wildcard found in excludes | ||
| * , otherwise return filter using {@link XContentParser} | ||
| */ | ||
| static XContentFieldFilter newFieldFilter(String[] includes, String[] excludes) { | ||
| if ((CollectionUtils.isEmpty(excludes) == false) && Arrays.stream(excludes).filter(field -> field.contains("*")).count() > 0) { | ||
| return (originalSource, contentType) -> { | ||
| Function<Map<String, ?>, Map<String, Object>> mapFilter = XContentMapValues.filter(includes, excludes); | ||
| Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(originalSource, true, contentType); | ||
| Map<String, Object> filteredSource = mapFilter.apply(mapTuple.v2()); | ||
| BytesStreamOutput bStream = new BytesStreamOutput(); | ||
| XContentType actualContentType = mapTuple.v1(); | ||
| XContentBuilder builder = XContentFactory.contentBuilder(actualContentType, bStream).map(filteredSource); | ||
| builder.close(); | ||
| return bStream.bytes(); | ||
| }; | ||
| } else { | ||
| final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering( | ||
| Set.of(includes), | ||
| Set.of(excludes) | ||
| ); | ||
| return (originalSource, contentType) -> { | ||
| if (contentType == null) { | ||
| contentType = XContentHelper.xContentTypeMayCompressed(originalSource); | ||
| } | ||
| BytesStreamOutput streamOutput = new BytesStreamOutput(Math.min(1024, originalSource.length())); | ||
| XContentBuilder builder = new XContentBuilder(contentType.xContent(), streamOutput); | ||
| XContentParser parser = contentType.xContent().createParser(parserConfig, originalSource.streamInput()); | ||
| builder.copyCurrentStructure(parser); | ||
| return BytesReference.bytes(builder); | ||
| }; | ||
|
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. Compressed source not decompressed before parsing in filterHigh Severity The parser-based filtering branch detects content type using |
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


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.
XContentParser resource leak in parser-based filtering branch
Medium Severity
The
XContentParsercreated at line 70 is never closed.XContentParserextendsCloseableand wraps an input stream that needs proper cleanup. This resource leak occurs every time the filter is applied when excludes doesn't contain wildcards. The codebase pattern (visible inIndexAbstraction.javaandIndexRouting.java) uses try-with-resources for parsers created withstreamInput(), but this implementation omits that.