-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Add Highlighter for Semantic Text Fields #118064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
534a96a
Add Highlighter for Semantic Text Fields
jimczi 5cfc185
Update docs/changelog/118064.yaml
jimczi 78ab7a8
add a note regarding the fragments order
jimczi efbdefc
add missing plugin in tests
jimczi 4af07d1
fix field name for sparse vector query
jimczi 0a20ad4
Merge remote-tracking branch 'upstream/main' into semantic_highlighter
jimczi a832939
spotless
jimczi 4639c31
add yml tests and improve error message for invalid format
jimczi 39896d7
Merge remote-tracking branch 'upstream/main' into semantic_highlighter
jimczi ecd7cce
line len
jimczi e5b4ce0
Merge remote-tracking branch 'upstream/main' into semantic_highlighter
jimczi e5da3fd
fix String#format
jimczi 68f5bb6
Merge remote-tracking branch 'upstream/main' into semantic_highlighter
jimczi d2a62a2
spotless
jimczi f149399
Merge remote-tracking branch 'upstream/main' into semantic_highlighter
jimczi e0d1fe5
Merge branch 'main' into semantic_highlighter
jimczi 28a06f6
Merge remote-tracking branch 'upstream/main' into semantic_highlighter
jimczi 13499d3
fix another test
jimczi d8e383b
Merge remote-tracking branch 'origin/semantic_highlighter' into seman…
jimczi c8679d7
fix tests for good?
jimczi 236c6a3
line len
jimczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 118064 | ||
summary: Add Highlighter for Semantic Text Fields | ||
area: Highlighting | ||
type: feature | ||
issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
226 changes: 226 additions & 0 deletions
226
...ce/src/main/java/org/elasticsearch/xpack/inference/highlight/SemanticTextHighlighter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.inference.highlight; | ||
|
||
import org.apache.lucene.index.LeafReader; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.BooleanClause; | ||
import org.apache.lucene.search.BooleanQuery; | ||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.KnnByteVectorQuery; | ||
import org.apache.lucene.search.KnnFloatVectorQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.QueryVisitor; | ||
import org.apache.lucene.search.ScoreMode; | ||
import org.apache.lucene.search.Scorer; | ||
import org.apache.lucene.search.Weight; | ||
import org.elasticsearch.common.text.Text; | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.DenseVectorFieldType; | ||
import org.elasticsearch.index.mapper.vectors.SparseVectorFieldMapper.SparseVectorFieldType; | ||
import org.elasticsearch.index.query.SearchExecutionContext; | ||
import org.elasticsearch.search.fetch.subphase.highlight.FieldHighlightContext; | ||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; | ||
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter; | ||
import org.elasticsearch.search.vectors.VectorData; | ||
import org.elasticsearch.xpack.core.ml.search.SparseVectorQueryWrapper; | ||
import org.elasticsearch.xpack.inference.mapper.SemanticTextField; | ||
import org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
/** | ||
* A {@link Highlighter} designed for the {@link SemanticTextFieldMapper}. | ||
* This highlighter extracts semantic queries and evaluates them against each chunk produced by the semantic text field. | ||
* It returns the top-scoring chunks as snippets, optionally sorted by their scores. | ||
*/ | ||
public class SemanticTextHighlighter implements Highlighter { | ||
public static final String NAME = "semantic"; | ||
|
||
private record OffsetAndScore(int offset, float score) {} | ||
|
||
@Override | ||
public boolean canHighlight(MappedFieldType fieldType) { | ||
if (fieldType instanceof SemanticTextFieldMapper.SemanticTextFieldType) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public HighlightField highlight(FieldHighlightContext fieldContext) throws IOException { | ||
SemanticTextFieldMapper.SemanticTextFieldType fieldType = (SemanticTextFieldMapper.SemanticTextFieldType) fieldContext.fieldType; | ||
if (fieldType.getEmbeddingsField() == null) { | ||
// nothing indexed yet | ||
return null; | ||
} | ||
|
||
final List<Query> queries = switch (fieldType.getModelSettings().taskType()) { | ||
case SPARSE_EMBEDDING -> extractSparseVectorQueries( | ||
(SparseVectorFieldType) fieldType.getEmbeddingsField().fieldType(), | ||
fieldContext.query | ||
); | ||
case TEXT_EMBEDDING -> extractDenseVectorQueries( | ||
(DenseVectorFieldType) fieldType.getEmbeddingsField().fieldType(), | ||
fieldContext.query | ||
); | ||
default -> throw new IllegalStateException( | ||
"Wrong task type for a semantic text field, got [" + fieldType.getModelSettings().taskType().name() + "]" | ||
); | ||
}; | ||
Mikep86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (queries.isEmpty()) { | ||
// nothing to highlight | ||
return null; | ||
} | ||
|
||
int numberOfFragments = fieldContext.field.fieldOptions().numberOfFragments() <= 0 | ||
? 1 // we return the best fragment by default | ||
: fieldContext.field.fieldOptions().numberOfFragments(); | ||
|
||
List<OffsetAndScore> chunks = extractOffsetAndScores( | ||
fieldContext.context.getSearchExecutionContext(), | ||
fieldContext.hitContext.reader(), | ||
fieldType, | ||
fieldContext.hitContext.docId(), | ||
queries | ||
); | ||
if (chunks.size() == 0) { | ||
return null; | ||
} | ||
|
||
chunks.sort(Comparator.comparingDouble(OffsetAndScore::score).reversed()); | ||
int size = Math.min(chunks.size(), numberOfFragments); | ||
if (fieldContext.field.fieldOptions().scoreOrdered() == false) { | ||
chunks = chunks.subList(0, size); | ||
chunks.sort(Comparator.comparingInt(c -> c.offset)); | ||
} | ||
Mikep86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Text[] snippets = new Text[size]; | ||
List<Map<?, ?>> nestedSources = XContentMapValues.extractNestedSources( | ||
fieldType.getChunksField().fullPath(), | ||
fieldContext.hitContext.source().source() | ||
); | ||
for (int i = 0; i < size; i++) { | ||
var chunk = chunks.get(i); | ||
if (nestedSources.size() <= chunk.offset) { | ||
throw new IllegalStateException( | ||
String.format( | ||
Locale.ROOT, | ||
"Invalid content detected for field [%s]: the chunks size is [%d], " | ||
+ "but a reference to offset [%d] was found in the result.", | ||
fieldType.name(), | ||
nestedSources.size(), | ||
chunk.offset | ||
) | ||
); | ||
} | ||
String content = (String) nestedSources.get(chunk.offset).get(SemanticTextField.CHUNKED_TEXT_FIELD); | ||
if (content == null) { | ||
throw new IllegalStateException( | ||
String.format( | ||
Locale.ROOT, | ||
|
||
"Invalid content detected for field [%s]: missing text for the chunk at offset [%d].", | ||
fieldType.name(), | ||
chunk.offset | ||
) | ||
); | ||
} | ||
snippets[i] = new Text(content); | ||
} | ||
return new HighlightField(fieldContext.fieldName, snippets); | ||
} | ||
|
||
private List<OffsetAndScore> extractOffsetAndScores( | ||
SearchExecutionContext context, | ||
LeafReader reader, | ||
SemanticTextFieldMapper.SemanticTextFieldType fieldType, | ||
int docId, | ||
List<Query> leafQueries | ||
) throws IOException { | ||
var bitSet = context.bitsetFilter(fieldType.getChunksField().parentTypeFilter()).getBitSet(reader.getContext()); | ||
int previousParent = docId > 0 ? bitSet.prevSetBit(docId - 1) : -1; | ||
|
||
BooleanQuery.Builder bq = new BooleanQuery.Builder().add(fieldType.getChunksField().nestedTypeFilter(), BooleanClause.Occur.FILTER); | ||
leafQueries.stream().forEach(q -> bq.add(q, BooleanClause.Occur.SHOULD)); | ||
Weight weight = new IndexSearcher(reader).createWeight(bq.build(), ScoreMode.COMPLETE, 1); | ||
Scorer scorer = weight.scorer(reader.getContext()); | ||
if (previousParent != -1) { | ||
if (scorer.iterator().advance(previousParent) == DocIdSetIterator.NO_MORE_DOCS) { | ||
return List.of(); | ||
} | ||
} else if (scorer.iterator().nextDoc() == DocIdSetIterator.NO_MORE_DOCS) { | ||
return List.of(); | ||
} | ||
List<OffsetAndScore> results = new ArrayList<>(); | ||
int offset = 0; | ||
while (scorer.docID() < docId) { | ||
results.add(new OffsetAndScore(offset++, scorer.score())); | ||
if (scorer.iterator().nextDoc() == DocIdSetIterator.NO_MORE_DOCS) { | ||
break; | ||
} | ||
} | ||
return results; | ||
} | ||
|
||
private List<Query> extractDenseVectorQueries(DenseVectorFieldType fieldType, Query querySection) { | ||
// TODO: Handle knn section when semantic text field can be used. | ||
List<Query> queries = new ArrayList<>(); | ||
querySection.visit(new QueryVisitor() { | ||
@Override | ||
public boolean acceptField(String field) { | ||
return fieldType.name().equals(field); | ||
} | ||
|
||
@Override | ||
public void consumeTerms(Query query, Term... terms) { | ||
super.consumeTerms(query, terms); | ||
} | ||
|
||
@Override | ||
public void visitLeaf(Query query) { | ||
if (query instanceof KnnFloatVectorQuery knnQuery) { | ||
queries.add(fieldType.createExactKnnQuery(VectorData.fromFloats(knnQuery.getTargetCopy()), null)); | ||
} else if (query instanceof KnnByteVectorQuery knnQuery) { | ||
queries.add(fieldType.createExactKnnQuery(VectorData.fromBytes(knnQuery.getTargetCopy()), null)); | ||
} | ||
} | ||
}); | ||
return queries; | ||
} | ||
|
||
private List<Query> extractSparseVectorQueries(SparseVectorFieldType fieldType, Query querySection) { | ||
List<Query> queries = new ArrayList<>(); | ||
querySection.visit(new QueryVisitor() { | ||
@Override | ||
public boolean acceptField(String field) { | ||
return fieldType.name().equals(field); | ||
} | ||
|
||
@Override | ||
public void consumeTerms(Query query, Term... terms) { | ||
super.consumeTerms(query, terms); | ||
} | ||
|
||
@Override | ||
public QueryVisitor getSubVisitor(BooleanClause.Occur occur, Query parent) { | ||
if (parent instanceof SparseVectorQueryWrapper sparseVectorQuery) { | ||
queries.add(sparseVectorQuery.getTermsQuery()); | ||
} | ||
return this; | ||
} | ||
}); | ||
return queries; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.