diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/search/fetch/subphase/FetchSourcePhaseBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/search/fetch/subphase/FetchSourcePhaseBenchmark.java index 848ee6e556dc..55b8c18138f4 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/search/fetch/subphase/FetchSourcePhaseBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/search/fetch/subphase/FetchSourcePhaseBenchmark.java @@ -63,7 +63,7 @@ public void setup() throws IOException { ); includesSet = Set.of(fetchContext.includes()); excludesSet = Set.of(fetchContext.excludes()); - parserConfig = XContentParserConfiguration.EMPTY.withFiltering(includesSet, excludesSet, false); + parserConfig = XContentParserConfiguration.EMPTY.withFiltering(null, includesSet, excludesSet, false); } private BytesReference read300BytesExample() throws IOException { diff --git a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java index 334f5ef15304..aa9236e9f314 100644 --- a/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java +++ b/benchmarks/src/main/java/org/elasticsearch/benchmark/xcontent/FilterContentBenchmark.java @@ -170,7 +170,7 @@ private XContentParserConfiguration buildParseConfig(boolean matchDotsInFieldNam includes = null; excludes = filters; } - return XContentParserConfiguration.EMPTY.withFiltering(includes, excludes, matchDotsInFieldNames); + return XContentParserConfiguration.EMPTY.withFiltering(null, includes, excludes, matchDotsInFieldNames); } private BytesReference filter(XContentParserConfiguration contentParserConfiguration) throws IOException { diff --git a/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/XContentParserConfigurationImpl.java b/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/XContentParserConfigurationImpl.java index 1632cd810a10..53089af93c13 100644 --- a/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/XContentParserConfigurationImpl.java +++ b/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/XContentParserConfigurationImpl.java @@ -104,7 +104,7 @@ public RestApiVersion restApiVersion() { } public XContentParserConfiguration withFiltering( - String rootPath, + String prefixPath, Set includeStrings, Set excludeStrings, boolean filtersMatchFieldNamesWithDots @@ -112,11 +112,11 @@ public XContentParserConfiguration withFiltering( FilterPath[] includePaths = FilterPath.compile(includeStrings); FilterPath[] excludePaths = FilterPath.compile(excludeStrings); - if (rootPath != null) { + if (prefixPath != null) { if (includePaths != null) { List includeFilters = new ArrayList<>(); for (var incl : includePaths) { - incl.matches(rootPath, includeFilters, true); + incl.matches(prefixPath, includeFilters, true); } includePaths = includeFilters.isEmpty() ? null : includeFilters.toArray(FilterPath[]::new); } @@ -124,7 +124,7 @@ public XContentParserConfiguration withFiltering( if (excludePaths != null) { List excludeFilters = new ArrayList<>(); for (var excl : excludePaths) { - excl.matches(rootPath, excludeFilters, true); + excl.matches(prefixPath, excludeFilters, true); } excludePaths = excludeFilters.isEmpty() ? null : excludeFilters.toArray(FilterPath[]::new); } @@ -139,14 +139,6 @@ public XContentParserConfiguration withFiltering( ); } - public XContentParserConfiguration withFiltering( - Set includeStrings, - Set excludeStrings, - boolean filtersMatchFieldNamesWithDots - ) { - return withFiltering(null, includeStrings, excludeStrings, filtersMatchFieldNamesWithDots); - } - public JsonParser filter(JsonParser parser) { JsonParser filtered = parser; if (excludes != null) { diff --git a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParserConfiguration.java b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParserConfiguration.java index f50875105056..8527062ef145 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParserConfiguration.java +++ b/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentParserConfiguration.java @@ -51,15 +51,18 @@ public interface XContentParserConfiguration { /** * Replace the configured filtering. + * + * @param prefixPath The prefix path to be appended to each sub-path before applying the include/exclude rules. + * Specify {@code null} if parsing starts from the root. + * @param includeStrings A set of strings representing paths to include during filtering. + * If specified, only these paths will be included in parsing. + * @param excludeStrings A set of strings representing paths to exclude during filtering. + * If specified, these paths will be excluded from parsing. + * @param filtersMatchFieldNamesWithDots Indicates whether filters should match field names containing dots ('.') + * as part of the field name. */ XContentParserConfiguration withFiltering( - Set includeStrings, - Set excludeStrings, - boolean filtersMatchFieldNamesWithDots - ); - - XContentParserConfiguration withFiltering( - String rootPath, + String prefixPath, Set includeStrings, Set excludeStrings, boolean filtersMatchFieldNamesWithDots diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java index dd4a52fd9bed..7a05230e489f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java @@ -1361,6 +1361,7 @@ public DataStream getParentDataStream() { } public static final XContentParserConfiguration TS_EXTRACT_CONFIG = XContentParserConfiguration.EMPTY.withFiltering( + null, Set.of(TIMESTAMP_FIELD_NAME), null, false diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java b/server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java index 3fb3c182f89c..9699afd0d813 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java @@ -268,7 +268,7 @@ public static class ExtractFromSource extends IndexRouting { trackTimeSeriesRoutingHash = metadata.getCreationVersion().onOrAfter(IndexVersions.TIME_SERIES_ROUTING_HASH_IN_ID); List routingPaths = metadata.getRoutingPaths(); isRoutingPath = Regex.simpleMatcher(routingPaths.toArray(String[]::new)); - this.parserConfig = XContentParserConfiguration.EMPTY.withFiltering(Set.copyOf(routingPaths), null, true); + this.parserConfig = XContentParserConfiguration.EMPTY.withFiltering(null, Set.copyOf(routingPaths), null, true); } public boolean matchesField(String fieldName) { diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 9cfa22d0a3cf..40451fe0eafd 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -191,7 +191,7 @@ public static Tuple> convertToMap( ) throws ElasticsearchParseException { XContentParserConfiguration config = XContentParserConfiguration.EMPTY; if (include != null || exclude != null) { - config = config.withFiltering(include, exclude, false); + config = config.withFiltering(null, include, exclude, false); } return parseToType(ordered ? XContentParser::mapOrdered : XContentParser::map, bytes, xContentType, config); } @@ -266,7 +266,7 @@ public static Map convertToMap( @Nullable Set exclude ) throws ElasticsearchParseException { try ( - XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY.withFiltering(include, exclude, false), input) + XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY.withFiltering(null, include, exclude, false), input) ) { return ordered ? parser.mapOrdered() : parser.map(); } catch (IOException e) { @@ -301,7 +301,7 @@ public static Map convertToMap( ) throws ElasticsearchParseException { try ( XContentParser parser = xContent.createParser( - XContentParserConfiguration.EMPTY.withFiltering(include, exclude, false), + XContentParserConfiguration.EMPTY.withFiltering(null, include, exclude, false), bytes, offset, length diff --git a/server/src/main/java/org/elasticsearch/search/lookup/SourceFilter.java b/server/src/main/java/org/elasticsearch/search/lookup/SourceFilter.java index ec22a14c558e..90034ef447c9 100644 --- a/server/src/main/java/org/elasticsearch/search/lookup/SourceFilter.java +++ b/server/src/main/java/org/elasticsearch/search/lookup/SourceFilter.java @@ -138,6 +138,7 @@ private Function buildBytesFilter() { return this::filterMap; } final XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering( + null, Set.copyOf(Arrays.asList(includes)), Set.copyOf(Arrays.asList(excludes)), true diff --git a/server/src/test/java/org/elasticsearch/index/mapper/XContentDataHelperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/XContentDataHelperTests.java index b20e77befa03..8f5a3684ea56 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/XContentDataHelperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/XContentDataHelperTests.java @@ -169,7 +169,7 @@ public void testObjectWithFilter() throws IOException { XContentParser p = createParser(JsonXContent.jsonXContent, object); assertThat(p.nextToken(), equalTo(XContentParser.Token.START_OBJECT)); - XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering(null, Set.of("path.filter.field"), true); + XContentParserConfiguration parserConfig = XContentParserConfiguration.EMPTY.withFiltering(null, null, Set.of("path.filter.field"), true); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.humanReadable(true); XContentDataHelper.decodeAndWrite(parserConfig, builder, XContentDataHelper.encodeToken(p)); diff --git a/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/action/TransportPutRollupJobAction.java b/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/action/TransportPutRollupJobAction.java index 6618f3199deb..3035bb98a3a9 100644 --- a/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/action/TransportPutRollupJobAction.java +++ b/x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/rollup/action/TransportPutRollupJobAction.java @@ -68,6 +68,7 @@ public class TransportPutRollupJobAction extends AcknowledgedTransportMasterNode private static final Logger LOGGER = LogManager.getLogger(TransportPutRollupJobAction.class); private static final XContentParserConfiguration PARSER_CONFIGURATION = XContentParserConfiguration.EMPTY.withFiltering( + null, Set.of("_doc._meta._rollup"), null, false