Skip to content

Commit

Permalink
simplify source filter creation
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Sep 30, 2024
1 parent 51b97c8 commit 6e342b9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 + "]"
);
Expand All @@ -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;
Expand Down

0 comments on commit 6e342b9

Please sign in to comment.