Skip to content

Commit 0bcd6ae

Browse files
authored
Avoid UOE when fetching the _inference metadata field (#135432)
The fields list in the context can be frozen so this change ensures that we recreate the fetch fields context when adding the _inference metadata field.
1 parent c94f366 commit 0bcd6ae

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,13 @@ private SearchHits buildSearchHits(SearchContext context, int[] docIdsToLoad, Pr
137137
if (lookup.inferenceFields().isEmpty() == false
138138
&& shouldExcludeInferenceFieldsFromSource(context.indexShard().indexSettings(), context.fetchSourceContext()) == false) {
139139
// Rehydrate the inference fields into the {@code _source} because they were explicitly requested.
140-
var fetchFieldsContext = context.fetchFieldsContext();
141-
if (fetchFieldsContext == null) {
142-
fetchFieldsContext = new FetchFieldsContext(new ArrayList<>());
140+
var oldFetchFieldsContext = context.fetchFieldsContext();
141+
var newFetchFieldsContext = new FetchFieldsContext(new ArrayList<>());
142+
if (oldFetchFieldsContext != null) {
143+
newFetchFieldsContext.fields().addAll(oldFetchFieldsContext.fields());
143144
}
144-
fetchFieldsContext.fields().add(new FieldAndFormat(InferenceMetadataFieldsMapper.NAME, null));
145-
context.fetchFieldsContext(fetchFieldsContext);
145+
newFetchFieldsContext.fields().add(new FieldAndFormat(InferenceMetadataFieldsMapper.NAME, null));
146+
context.fetchFieldsContext(newFetchFieldsContext);
146147
}
147148

148149
SourceLoader sourceLoader = context.newSourceLoader(res.v2());

x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/30_semantic_text_inference.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,3 +1347,42 @@ setup:
13471347

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

1350+
---
1351+
"Search can rehydrate the inference metadata fields in _source":
1352+
- do:
1353+
index:
1354+
index: test-index
1355+
id: doc_1
1356+
refresh: true
1357+
body:
1358+
sparse_field: "inference test"
1359+
dense_field: "another inference test"
1360+
non_inference_field: "non inference test"
1361+
1362+
- do:
1363+
search:
1364+
index: test-index
1365+
body:
1366+
_source:
1367+
exclude_vectors: false
1368+
fields: ["sparse_field", "dense_field", "non_inference_field"]
1369+
query:
1370+
term:
1371+
_id: doc_1
1372+
1373+
- match: { hits.total.value: 1 }
1374+
- match: { hits.hits.0.fields.sparse_field: ["inference test"]}
1375+
- match: { hits.hits.0.fields.dense_field: ["another inference test"]}
1376+
- match: { hits.hits.0.fields.non_inference_field: ["non inference test"]}
1377+
- not_exists: hits.hits.0.fields._inference_fields
1378+
- length: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks: 1 }
1379+
- length: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field: 1 }
1380+
- exists: hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field.0.embeddings
1381+
- match: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field.0.start_offset: 0 }
1382+
- match: { hits.hits.0._source._inference_fields.sparse_field.inference.chunks.sparse_field.0.end_offset: 14 }
1383+
- length: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field: 1 }
1384+
- exists: hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.embeddings
1385+
- match: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.start_offset: 0 }
1386+
- match: { hits.hits.0._source._inference_fields.dense_field.inference.chunks.dense_field.0.end_offset: 22 }
1387+
1388+

0 commit comments

Comments
 (0)