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
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ private SearchHits buildSearchHits(SearchContext context, int[] docIdsToLoad, Pr
if (lookup.inferenceFields().isEmpty() == false
&& shouldExcludeInferenceFieldsFromSource(context.indexShard().indexSettings(), context.fetchSourceContext()) == false) {
// Rehydrate the inference fields into the {@code _source} because they were explicitly requested.
var fetchFieldsContext = context.fetchFieldsContext();
if (fetchFieldsContext == null) {
fetchFieldsContext = new FetchFieldsContext(new ArrayList<>());
var oldFetchFieldsContext = context.fetchFieldsContext();
var newFetchFieldsContext = new FetchFieldsContext(new ArrayList<>());
if (oldFetchFieldsContext != null) {
newFetchFieldsContext.fields().addAll(oldFetchFieldsContext.fields());
}
fetchFieldsContext.fields().add(new FieldAndFormat(InferenceMetadataFieldsMapper.NAME, null));
context.fetchFieldsContext(fetchFieldsContext);
newFetchFieldsContext.fields().add(new FieldAndFormat(InferenceMetadataFieldsMapper.NAME, null));
context.fetchFieldsContext(newFetchFieldsContext);
}

SourceLoader sourceLoader = context.newSourceLoader(res.v2());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1347,3 +1347,42 @@ setup:

- match: { hits.total.value: 1 }

---
"Search can rehydrate the inference metadata fields in _source":
- do:
index:
index: test-index
id: doc_1
refresh: true
body:
sparse_field: "inference test"
dense_field: "another inference test"
non_inference_field: "non inference test"

- do:
search:
index: test-index
body:
_source:
exclude_vectors: false
fields: ["sparse_field", "dense_field", "non_inference_field"]
query:
term:
_id: doc_1

- match: { hits.total.value: 1 }
- match: { hits.hits.0.fields.sparse_field: ["inference test"]}
- match: { hits.hits.0.fields.dense_field: ["another inference test"]}
- match: { hits.hits.0.fields.non_inference_field: ["non inference test"]}
- not_exists: hits.hits.0.fields._inference_fields
- 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 }