2424import org .elasticsearch .index .IndexVersions ;
2525import org .elasticsearch .index .mapper .MappedFieldType ;
2626import org .elasticsearch .index .mapper .MappingLookup ;
27- import org .elasticsearch .index .mapper .ValueFetcher ;
2827import org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper .DenseVectorFieldType ;
2928import org .elasticsearch .index .mapper .vectors .SparseVectorFieldMapper .SparseVectorFieldType ;
3029import org .elasticsearch .index .query .SearchExecutionContext ;
4140import org .elasticsearch .xpack .inference .mapper .SemanticTextUtils ;
4241
4342import java .io .IOException ;
44- import java .io .UncheckedIOException ;
4543import java .util .ArrayList ;
4644import java .util .Comparator ;
4745import 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