diff --git a/server/src/main/java/org/elasticsearch/action/update/UpdateHelper.java b/server/src/main/java/org/elasticsearch/action/update/UpdateHelper.java index 212b99ca140d..30c6d09df393 100644 --- a/server/src/main/java/org/elasticsearch/action/update/UpdateHelper.java +++ b/server/src/main/java/org/elasticsearch/action/update/UpdateHelper.java @@ -36,6 +36,7 @@ import org.elasticsearch.script.UpdateScript; import org.elasticsearch.script.UpsertCtxMap; import org.elasticsearch.search.lookup.Source; +import org.elasticsearch.search.lookup.SourceFilter; import org.elasticsearch.xcontent.XContentType; import java.io.IOException; @@ -347,8 +348,9 @@ public static GetResult extractGetResult( return null; } BytesReference sourceFilteredAsBytes = sourceAsBytes; - if (request.fetchSource().hasFilter()) { - sourceFilteredAsBytes = Source.fromMap(source, sourceContentType).filter(request.fetchSource().filter()).internalSourceRef(); + SourceFilter sourceFilter = request.fetchSource().filter(); + if (sourceFilter != null) { + sourceFilteredAsBytes = Source.fromMap(source, sourceContentType).filter(sourceFilter).internalSourceRef(); } // TODO when using delete/none, we can still return the source as bytes by generating it (using the sourceContentType) diff --git a/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java b/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java index be0a3b8d851a..388c8cebfa83 100644 --- a/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java +++ b/server/src/main/java/org/elasticsearch/index/get/ShardGetService.java @@ -308,13 +308,10 @@ private GetResult innerGetFetch( DocIdAndVersion docIdAndVersion = get.docIdAndVersion(); SourceLoader loader = forceSyntheticSource ? new SourceLoader.Synthetic( - () -> mappingLookup.getMapping().syntheticFieldLoader(fetchSourceContext.hasFilter() ? fetchSourceContext.filter() : null), + () -> mappingLookup.getMapping().syntheticFieldLoader(fetchSourceContext.filter()), mapperMetrics.sourceFieldMetrics() ) - : mappingLookup.newSourceLoader( - fetchSourceContext.hasFilter() ? fetchSourceContext.filter() : null, - mapperMetrics.sourceFieldMetrics() - ); + : mappingLookup.newSourceLoader(fetchSourceContext.filter(), mapperMetrics.sourceFieldMetrics()); StoredFieldLoader storedFieldLoader = buildStoredFieldLoader(storedFields, fetchSourceContext, loader); LeafStoredFieldLoader leafStoredFieldLoader = storedFieldLoader.getLoader(docIdAndVersion.reader.getContext(), null); try { diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourceContext.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourceContext.java index 126c7aa28f4d..0594fa490978 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourceContext.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourceContext.java @@ -85,12 +85,15 @@ public String[] excludes() { return this.excludes; } - public boolean hasFilter() { + private boolean hasFilter() { return this.includes.length > 0 || this.excludes.length > 0; } + /** + * Returns a {@link SourceFilter} if filtering is enabled, {@code null} otherwise. + */ public SourceFilter filter() { - return new SourceFilter(includes, excludes); + return hasFilter() ? new SourceFilter(includes, excludes) : null; } public static FetchSourceContext parseFromRestRequest(RestRequest request) { diff --git a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java index e151f0fc2e09..3db539b91aff 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/subphase/FetchSourcePhase.java @@ -29,7 +29,7 @@ public FetchSubPhaseProcessor getProcessor(FetchContext fetchContext) { } assert fetchSourceContext.fetchSource(); SourceFilter sourceFilter = fetchSourceContext.filter(); - final boolean filterExcludesAll = sourceFilter.excludesAll(); + final boolean filterExcludesAll = sourceFilter != null && sourceFilter.excludesAll(); return new FetchSubPhaseProcessor() { private int fastPath; @@ -47,7 +47,7 @@ public StoredFieldsSpec storedFieldsSpec() { public void process(HitContext hitContext) { String index = fetchContext.getIndexName(); if (fetchContext.getSearchExecutionContext().isSourceEnabled() == false) { - if (fetchSourceContext.hasFilter()) { + if (sourceFilter != null) { throw new IllegalArgumentException( "unable to fetch fields from _source field: _source is disabled in the mappings for index [" + index + "]" ); @@ -62,7 +62,7 @@ private void hitExecute(FetchSourceContext fetchSourceContext, HitContext hitCon Source source = hitContext.source(); // If this is a parent document and there are no source filters, then add the source as-is. - if (nestedHit == false && fetchSourceContext.hasFilter() == false) { + if (nestedHit == false && sourceFilter == null) { hitContext.hit().sourceRef(source.internalSourceRef()); fastPath++; return;