Skip to content

Commit b9229eb

Browse files
authored
Bugfix/disable matches highlight knn (elastic#136563)
* Disable matches mode for all knn queries matches highlight mode will utilize a MemoryIndex. Newer knn queries utilize an optimization that makes assumptions about the IndexSearch and reader utilized. This will end up throwing an NPE. While we have fixed this NPE in Apache Lucene, we just should not utilize weight matches model for knn queries at all. Maybe this restriction can be addressed later. closes: elastic#136427 * adding comment * formatting * Update docs/changelog/136563.yaml
1 parent 59e02ae commit b9229eb

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

docs/changelog/136563.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 136563
2+
summary: Bugfix/disable matches highlight knn
3+
area: Vector Search
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/lucene/search/uhighlight/CustomUnifiedHighlighter.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@
3535
import org.elasticsearch.index.search.ESToParentBlockJoinQuery;
3636
import org.elasticsearch.search.retriever.rankdoc.RankDocsQuery;
3737
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;
3842
import org.elasticsearch.search.vectors.KnnScoreDocQuery;
43+
import org.elasticsearch.search.vectors.RescoreKnnVectorQuery;
44+
import org.elasticsearch.search.vectors.VectorSimilarityQuery;
3945

4046
import java.io.IOException;
4147
import java.text.BreakIterator;
@@ -56,6 +62,20 @@
5662
* Supports both returning empty snippets and non highlighted snippets when no highlighting can be performed.
5763
*/
5864
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+
5979
public static final char MULTIVAL_SEP_CHAR = (char) 0;
6080
private static final Snippet[] EMPTY_SNIPPET = new Snippet[0];
6181

@@ -259,8 +279,9 @@ public void visitLeaf(Query leafQuery) {
259279
/**
260280
* KnnScoreDocQuery and RankDocsQuery requires the same reader that built the docs
261281
* 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
262283
*/
263-
if (leafQuery instanceof KnnScoreDocQuery || leafQuery instanceof RankDocsQuery.TopQuery) {
284+
if (leafQuery instanceof RankDocsQuery.TopQuery || isKnnQuery(leafQuery)) {
264285
hasUnknownLeaf[0] = true;
265286
}
266287
super.visitLeaf(query);

0 commit comments

Comments
 (0)