Skip to content

Commit 2cb7963

Browse files
committed
iter
1 parent 825d626 commit 2cb7963

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
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;
2728
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DenseVectorFieldType;
2829
import org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.SparseVectorFieldType;
2930
import org.elasticsearch.index.query.SearchExecutionContext;
@@ -40,6 +41,7 @@
4041
import org.elasticsearch.xpack.inference.mapper.SemanticTextUtils;
4142

4243
import java.io.IOException;
44+
import java.io.UncheckedIOException;
4345
import java.util.ArrayList;
4446
import java.util.Comparator;
4547
import java.util.HashMap;
@@ -117,7 +119,7 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
117119
Text[] snippets = new Text[size];
118120
for (int i = 0; i < size; i++) {
119121
var chunk = chunks.get(i);
120-
var content = inputs.computeIfAbsent(chunk.offset.field(), k -> extractFieldContent(fieldContext.hitContext, mappingLookup, k));
122+
var content = inputs.computeIfAbsent(chunk.offset.field(), k -> extractFieldContent(fieldContext.context.getSearchExecutionContext(), fieldContext.hitContext, mappingLookup, k));
121123
if (content == null) {
122124
throw new IllegalStateException("Missing content for field [" + chunk.offset.field() + "]");
123125
}
@@ -138,13 +140,20 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
138140
return new HighlightField(fieldContext.fieldName, snippets);
139141
}
140142

141-
private String extractFieldContent(FetchSubPhase.HitContext hitContext, MappingLookup mappingLookup, String sourceField) {
143+
private String extractFieldContent(SearchExecutionContext searchContext, FetchSubPhase.HitContext hitContext, MappingLookup mappingLookup, String sourceField) {
142144
var sourceFieldType = mappingLookup.getFieldType(sourceField);
143145
if (sourceFieldType == null) {
144146
return null;
145147
}
146-
Object sourceValue = hitContext.source().extractValue(sourceFieldType.name(), null);
147-
return sourceValue != null ? SemanticTextUtils.nodeStringValues(sourceFieldType.name(), sourceValue) : null;
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;
148157
}
149158

150159
private List<OffsetAndScore> extractOffsetAndScores(

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -528,14 +528,7 @@ public Query existsQuery(SearchExecutionContext context) {
528528
@Override
529529
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
530530
if (indexVersionCreated.onOrAfter(IndexVersions.INFERENCE_METADATA_FIELDS)) {
531-
if (format != null) {
532-
if (format.equalsIgnoreCase("inference") == false) {
533-
throw new IllegalArgumentException("Illegal format for field [" + name() + "], got " + format);
534-
}
535-
return valueFetcherWithInferenceResults(context::bitsetFilter, context.searcher());
536-
} else {
537-
return SourceValueFetcher.toString(name(), context, null);
538-
}
531+
return SourceValueFetcher.toString(name(), context, null);
539532
} else {
540533
// Redirect the fetcher to load the original values of the field
541534
return SourceValueFetcher.toString(getOriginalTextFieldName(name()), context, format);

0 commit comments

Comments
 (0)