Skip to content

Commit 7fb28b0

Browse files
authored
Disable weight_matches when kNN query is present (#101713) (#101715)
8.10 added a new flag called `weight_matches` and we use it by default when highlighting. However, every hybrid search with kNN will fail with cryptic errors. This PR disables weight_matches mode when kNN queries are present. Supporting weigh_matches & kNN will take more work. closes: #101667
1 parent 0c809a9 commit 7fb28b0

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

docs/changelog/101713.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 101713
2+
summary: Disable `weight_matches` when kNN query is present
3+
area: Highlighting
4+
type: bug
5+
issues: []

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.highlight/10_unified.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,50 @@ teardown:
9393
- match: {hits.hits.0.highlight.text.0: "The <em>quick</em> <em>brown</em> <em>fox</em> is brown."}
9494
- match: {hits.hits.0.highlight.text\.fvh.0: "The <em>quick</em> <em>brown</em> <em>fox</em> is brown."}
9595
- match: {hits.hits.0.highlight.text\.postings.0: "The <em>quick</em> <em>brown</em> <em>fox</em> is brown."}
96+
---
97+
"Test hybrid search with knn where automatically disables weighted mode":
98+
- skip:
99+
version: ' - 8.11.99'
100+
reason: 'kNN was not correctly skipped until 8.12'
101+
102+
- do:
103+
indices.create:
104+
index: test-highlighting-knn
105+
body:
106+
mappings:
107+
"properties":
108+
"vectors":
109+
"type": "dense_vector"
110+
"dims": 2
111+
"index": true
112+
"similarity": "l2_norm"
113+
"text":
114+
"type": "text"
115+
"fields":
116+
"fvh":
117+
"type": "text"
118+
"term_vector": "with_positions_offsets"
119+
"postings":
120+
"type": "text"
121+
"index_options": "offsets"
122+
- do:
123+
index:
124+
index: test-highlighting-knn
125+
id: "1"
126+
body:
127+
"text" : "The quick brown fox is brown."
128+
"vectors": [1, 2]
129+
- do:
130+
indices.refresh: {}
131+
132+
- do:
133+
search:
134+
index: test-highlighting-knn
135+
body: {
136+
"query": { "multi_match": { "query": "quick brown fox", "type": "phrase", "fields": [ "text*" ] } },
137+
"highlight": { "type": "unified", "fields": { "*": { } } },
138+
"knn": { "field": "vectors", "query_vector": [1, 2], "k": 10, "num_candidates": 10 } }
139+
140+
- match: { hits.hits.0.highlight.text.0: "The <em>quick</em> <em>brown</em> <em>fox</em> is brown." }
141+
- match: { hits.hits.0.highlight.text\.fvh.0: "The <em>quick</em> <em>brown</em> <em>fox</em> is brown." }
142+
- match: { hits.hits.0.highlight.text\.postings.0: "The <em>quick</em> <em>brown</em> <em>fox</em> is brown." }

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.index.IndexSettings;
3333
import org.elasticsearch.index.search.ESToParentBlockJoinQuery;
3434
import org.elasticsearch.search.runtime.AbstractScriptFieldQuery;
35+
import org.elasticsearch.search.vectors.KnnScoreDocQuery;
3536

3637
import java.io.IOException;
3738
import java.text.BreakIterator;
@@ -250,6 +251,13 @@ public void visitLeaf(Query leafQuery) {
250251
if (leafQuery.getClass().getSimpleName().equals("LateParsingQuery")) {
251252
hasUnknownLeaf[0] = true;
252253
}
254+
/**
255+
* KnnScoreDocQuery requires the same reader that built the docs
256+
* When using {@link HighlightFlag#WEIGHT_MATCHES} different readers are used and isn't supported by this query
257+
*/
258+
if (leafQuery instanceof KnnScoreDocQuery) {
259+
hasUnknownLeaf[0] = true;
260+
}
253261
super.visitLeaf(query);
254262
}
255263

0 commit comments

Comments
 (0)