Skip to content

Comments

Add XContentFieldFilter (#81970)#3

Open
MitchLewis930 wants to merge 1 commit intopr_013_beforefrom
pr_013_after
Open

Add XContentFieldFilter (#81970)#3
MitchLewis930 wants to merge 1 commit intopr_013_beforefrom
pr_013_after

Conversation

@MitchLewis930
Copy link

PR_013

This commit introduces XContentFieldFilter, which applies field includes/excludes to 
XContent without having to realise the xcontent itself as a java map.  SourceFieldMapper
and ShardGetService are cut over to use this class
@MitchLewis930 MitchLewis930 requested a review from Copilot January 31, 2026 00:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a new XContentFieldFilter interface to centralize and optimize source field filtering logic in Elasticsearch. The change replaces scattered filtering implementations with a unified approach that chooses between map-based filtering (for wildcards) and parser-based filtering (for exact matches).

Changes:

  • Introduces XContentFieldFilter interface with factory method for creating optimized filters
  • Refactors SourceFieldMapper to use the new filter interface instead of inline filtering logic
  • Updates ShardGetService to leverage the new filtering mechanism
  • Adds xContentTypeMayCompressed helper method to XContentHelper for content type detection

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
XContentFieldFilter.java New interface defining source filtering contract with factory method that selects optimal implementation
SourceFieldMapper.java Replaces inline filtering logic with XContentFieldFilter, simplifying the applyFilters method
ShardGetService.java Adopts XContentFieldFilter for source filtering in GET operations
XContentHelper.java Adds deprecated helper method for guessing compressed content types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

import java.util.function.Function;

/**
* A filter that filter fields away from source
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected grammar: 'A filter that filters fields' instead of 'A filter that filter fields'.

Suggested change
* A filter that filter fields away from source
* A filter that filters fields away from source

Copilot uses AI. Check for mistakes.
* , 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) {
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses count() > 0 on a stream to check if any element matches. Use anyMatch(field -> field.contains("*")) instead, which short-circuits and avoids counting all matches.

Suggested change
if ((CollectionUtils.isEmpty(excludes) == false) && Arrays.stream(excludes).filter(field -> field.contains("*")).count() > 0) {
if ((CollectionUtils.isEmpty(excludes) == false) && Arrays.stream(excludes).anyMatch(field -> field.contains("*"))) {

Copilot uses AI. Check for mistakes.
Comment on lines +60 to +62
final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering(
Set.of(includes),
Set.of(excludes)
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating sets from arrays without null checks could cause NullPointerException. If includes or excludes arrays are null, Set.of() will throw an exception. Consider using CollectionUtils or adding null guards.

Suggested change
final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering(
Set.of(includes),
Set.of(excludes)
final Set<String> includesSet = includes == null ? Set.of() : Set.of(includes);
final Set<String> excludesSet = excludes == null ? Set.of() : Set.of(excludes);
final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering(
includesSet,
excludesSet

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants