|
35 | 35 | import org.elasticsearch.index.search.ESToParentBlockJoinQuery; |
36 | 36 | import org.elasticsearch.search.retriever.rankdoc.RankDocsQuery; |
37 | 37 | import org.elasticsearch.search.runtime.AbstractScriptFieldQuery; |
| 38 | +import org.elasticsearch.search.vectors.ESDiversifyingChildrenByteKnnVectorQuery; |
| 39 | +import org.elasticsearch.search.vectors.ESDiversifyingChildrenFloatKnnVectorQuery; |
| 40 | +import org.elasticsearch.search.vectors.ESKnnByteVectorQuery; |
| 41 | +import org.elasticsearch.search.vectors.ESKnnFloatVectorQuery; |
38 | 42 | import org.elasticsearch.search.vectors.KnnScoreDocQuery; |
| 43 | +import org.elasticsearch.search.vectors.RescoreKnnVectorQuery; |
| 44 | +import org.elasticsearch.search.vectors.VectorSimilarityQuery; |
39 | 45 |
|
40 | 46 | import java.io.IOException; |
41 | 47 | import java.text.BreakIterator; |
|
56 | 62 | * Supports both returning empty snippets and non highlighted snippets when no highlighting can be performed. |
57 | 63 | */ |
58 | 64 | public final class CustomUnifiedHighlighter extends UnifiedHighlighter { |
| 65 | + private static boolean isKnnQuery(Query q) { |
| 66 | + // TODO: bug https://github.com/elastic/elasticsearch/issues/136427 |
| 67 | + // AbstractKnnVectorQuery is not public, so we need to list concrete classes |
| 68 | + // currently there is a bug in Lucene that causes things that use |
| 69 | + // AbstractKnnVectorQuery to throw NPEs in weight matches highlighting |
| 70 | + return q instanceof KnnScoreDocQuery |
| 71 | + || q instanceof RescoreKnnVectorQuery |
| 72 | + || q instanceof VectorSimilarityQuery |
| 73 | + || q instanceof ESKnnFloatVectorQuery |
| 74 | + || q instanceof ESKnnByteVectorQuery |
| 75 | + || q instanceof ESDiversifyingChildrenByteKnnVectorQuery |
| 76 | + || q instanceof ESDiversifyingChildrenFloatKnnVectorQuery; |
| 77 | + } |
| 78 | + |
59 | 79 | public static final char MULTIVAL_SEP_CHAR = (char) 0; |
60 | 80 | private static final Snippet[] EMPTY_SNIPPET = new Snippet[0]; |
61 | 81 |
|
@@ -259,8 +279,9 @@ public void visitLeaf(Query leafQuery) { |
259 | 279 | /** |
260 | 280 | * KnnScoreDocQuery and RankDocsQuery requires the same reader that built the docs |
261 | 281 | * When using {@link HighlightFlag#WEIGHT_MATCHES} different readers are used and isn't supported by this query |
| 282 | + * Additionally, kNN queries don't really work against MemoryIndex which is used in the matches API |
262 | 283 | */ |
263 | | - if (leafQuery instanceof KnnScoreDocQuery || leafQuery instanceof RankDocsQuery.TopQuery) { |
| 284 | + if (leafQuery instanceof RankDocsQuery.TopQuery || isKnnQuery(leafQuery)) { |
264 | 285 | hasUnknownLeaf[0] = true; |
265 | 286 | } |
266 | 287 | super.visitLeaf(query); |
|
0 commit comments