Skip to content

Commit

Permalink
iter
Browse files Browse the repository at this point in the history
  • Loading branch information
jimczi committed Dec 2, 2024
1 parent 825d626 commit 2cb7963
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.index.IndexVersions;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MappingLookup;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DenseVectorFieldType;
import org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.SparseVectorFieldType;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand All @@ -40,6 +41,7 @@
import org.elasticsearch.xpack.inference.mapper.SemanticTextUtils;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -117,7 +119,7 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
Text[] snippets = new Text[size];
for (int i = 0; i < size; i++) {
var chunk = chunks.get(i);
var content = inputs.computeIfAbsent(chunk.offset.field(), k -> extractFieldContent(fieldContext.hitContext, mappingLookup, k));
var content = inputs.computeIfAbsent(chunk.offset.field(), k -> extractFieldContent(fieldContext.context.getSearchExecutionContext(), fieldContext.hitContext, mappingLookup, k));
if (content == null) {
throw new IllegalStateException("Missing content for field [" + chunk.offset.field() + "]");
}
Expand All @@ -138,13 +140,20 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
return new HighlightField(fieldContext.fieldName, snippets);
}

private String extractFieldContent(FetchSubPhase.HitContext hitContext, MappingLookup mappingLookup, String sourceField) {
private String extractFieldContent(SearchExecutionContext searchContext, FetchSubPhase.HitContext hitContext, MappingLookup mappingLookup, String sourceField) {
var sourceFieldType = mappingLookup.getFieldType(sourceField);
if (sourceFieldType == null) {
return null;
}
Object sourceValue = hitContext.source().extractValue(sourceFieldType.name(), null);
return sourceValue != null ? SemanticTextUtils.nodeStringValues(sourceFieldType.name(), sourceValue) : null;
ValueFetcher fetcher = sourceFieldType.valueFetcher(searchContext, null);
fetcher.setNextReader(hitContext.readerContext());
List<Object> result = null;
try {
result = fetcher.fetchValues(hitContext.source(), hitContext.docId(), new ArrayList<>());
} catch (IOException exc) {
throw new UncheckedIOException(exc);
}
return result.size() > 0 ? SemanticTextUtils.nodeStringValues(sourceFieldType.name(), result) : null;
}

private List<OffsetAndScore> extractOffsetAndScores(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,14 +528,7 @@ public Query existsQuery(SearchExecutionContext context) {
@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
if (indexVersionCreated.onOrAfter(IndexVersions.INFERENCE_METADATA_FIELDS)) {
if (format != null) {
if (format.equalsIgnoreCase("inference") == false) {
throw new IllegalArgumentException("Illegal format for field [" + name() + "], got " + format);
}
return valueFetcherWithInferenceResults(context::bitsetFilter, context.searcher());
} else {
return SourceValueFetcher.toString(name(), context, null);
}
return SourceValueFetcher.toString(name(), context, null);
} else {
// Redirect the fetcher to load the original values of the field
return SourceValueFetcher.toString(getOriginalTextFieldName(name()), context, format);
Expand Down

0 comments on commit 2cb7963

Please sign in to comment.