Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/135897.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 135897
summary: Apply source excludes early when retrieving the `_inference_fields`
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ private static Boolean shouldExcludeVectorsFromSourceExplicit(FetchSourceContext

public static boolean shouldExcludeInferenceFieldsFromSource(IndexSettings indexSettings, FetchSourceContext fetchSourceContext) {
var explicit = shouldExcludeInferenceFieldsFromSourceExplicit(fetchSourceContext);
var filter = fetchSourceContext != null ? fetchSourceContext.filter() : null;
if (filter != null) {
if (filter.isPathFiltered(InferenceMetadataFieldsMapper.NAME, true)) {
return true;
} else if (filter.isExplicitlyIncluded(InferenceMetadataFieldsMapper.NAME)) {
return false;
}
}
return explicit != null ? explicit : INDEX_MAPPING_EXCLUDE_SOURCE_VECTORS_SETTING.get(indexSettings.getSettings());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.elasticsearch.search.lookup.Source;
import org.elasticsearch.search.lookup.SourceFilter;

import java.util.HashMap;
import java.util.Map;

public final class FetchSourcePhase implements FetchSubPhase {
Expand Down Expand Up @@ -99,6 +100,10 @@ private Source replaceInferenceMetadataFields(SearchHit hit, Source source) {
return source;
}
var newSource = source.source();
if (newSource instanceof HashMap == false) {
// the map is not mutable
newSource = new HashMap<>(newSource);
}
newSource.put(InferenceMetadataFieldsMapper.NAME, field.getValues().get(0));
return Source.fromMap(newSource, source.sourceContentType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1385,4 +1385,42 @@ setup:
- match: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.start_offset: 0 }
- match: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.end_offset: 22 }

- do:
search:
index: test-index
body:
_source:
exclude_vectors: false
excludes: ["*"]
query:
term:
_id: doc_1

- match: { hits.total.value: 1 }
- length: { hits.hits.0._source: 0}

- do:
search:
index: test-index
body:
_source:
exclude_vectors: false
excludes: ["*_field"]
query:
term:
_id: doc_1

- match: { hits.total.value: 1 }
- length: { hits.hits.0._source: 1}
- length: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks: 1 }
- length: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field: 1 }
- exists: hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field.0.embeddings
- match: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field.0.start_offset: 0 }
- match: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field.0.end_offset: 14 }
- length: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field: 1 }
- exists: hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.embeddings
- match: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.start_offset: 0 }
- match: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.end_offset: 22 }