Skip to content

Commit bcce2ea

Browse files
committed
Remove value fetcher as it also retrieves copy_to fields
1 parent 2cb7963 commit bcce2ea

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/highlight/SemanticTextHighlighter.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.elasticsearch.index.IndexVersions;
2525
import org.elasticsearch.index.mapper.MappedFieldType;
2626
import org.elasticsearch.index.mapper.MappingLookup;
27-
import org.elasticsearch.index.mapper.ValueFetcher;
2827
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DenseVectorFieldType;
2928
import org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.SparseVectorFieldType;
3029
import org.elasticsearch.index.query.SearchExecutionContext;
@@ -41,7 +40,6 @@
4140
import org.elasticsearch.xpack.inference.mapper.SemanticTextUtils;
4241

4342
import java.io.IOException;
44-
import java.io.UncheckedIOException;
4543
import java.util.ArrayList;
4644
import java.util.Comparator;
4745
import java.util.HashMap;
@@ -119,7 +117,7 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
119117
Text[] snippets = new Text[size];
120118
for (int i = 0; i < size; i++) {
121119
var chunk = chunks.get(i);
122-
var content = inputs.computeIfAbsent(chunk.offset.field(), k -> extractFieldContent(fieldContext.context.getSearchExecutionContext(), fieldContext.hitContext, mappingLookup, k));
120+
var content = inputs.computeIfAbsent(chunk.offset.field(), k -> extractFieldContent(fieldContext.hitContext, mappingLookup, k));
123121
if (content == null) {
124122
throw new IllegalStateException("Missing content for field [" + chunk.offset.field() + "]");
125123
}
@@ -140,20 +138,15 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
140138
return new HighlightField(fieldContext.fieldName, snippets);
141139
}
142140

143-
private String extractFieldContent(SearchExecutionContext searchContext, FetchSubPhase.HitContext hitContext, MappingLookup mappingLookup, String sourceField) {
141+
private String extractFieldContent(FetchSubPhase.HitContext hitContext, MappingLookup mappingLookup, String sourceField) {
144142
var sourceFieldType = mappingLookup.getFieldType(sourceField);
145143
if (sourceFieldType == null) {
146144
return null;
147145
}
148-
ValueFetcher fetcher = sourceFieldType.valueFetcher(searchContext, null);
149-
fetcher.setNextReader(hitContext.readerContext());
150-
List<Object> result = null;
151-
try {
152-
result = fetcher.fetchValues(hitContext.source(), hitContext.docId(), new ArrayList<>());
153-
} catch (IOException exc) {
154-
throw new UncheckedIOException(exc);
155-
}
156-
return result.size() > 0 ? SemanticTextUtils.nodeStringValues(sourceFieldType.name(), result) : null;
146+
// TODO: Consider using a value fetcher here, as it will work if the field is stored, but ensure it excludes values derived from
147+
// copy_to fields.
148+
Object sourceValue = hitContext.source().extractValue(sourceFieldType.name(), null);
149+
return sourceValue != null ? SemanticTextUtils.nodeStringValues(sourceFieldType.name(), sourceValue) : null;
157150
}
158151

159152
private List<OffsetAndScore> extractOffsetAndScores(

0 commit comments

Comments
 (0)