Skip to content

Commit a3d9e72

Browse files
authored
Fixing exists query REST tests for sparse_vector_fields (#100030) (#100397)
Removing (incorrectly) expected exceptions for `exists` queries `sparse_vectors` in < 8.0.0 versions. Closes #100003
1 parent 92c4a27 commit a3d9e72

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.vectors/90_sparse_vector.yml

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"Indexing and searching sparse vectors":
2+
"Indexing and searching sparse vectors in >=8.11":
33

44
- skip:
55
version: " - 8.10.99"
@@ -77,16 +77,24 @@
7777
index: test
7878
id: "3"
7979
body:
80-
text: "doing nothing will result in nothing"
80+
text: "empty array with no nested values - should not be retrieved in exists queries"
8181
ml:
82-
tokens: {}
82+
tokens: [ ]
83+
- do:
84+
index:
85+
index: test
86+
id: "4"
87+
body:
88+
text: "should still respond to exists queries if when empty"
89+
ml:
90+
tokens: { }
8391

8492
- match: { result: "created" }
8593

8694
- do:
8795
index:
8896
index: test
89-
id: "4"
97+
id: "5"
9098
body:
9199
text: "other embeddings available only"
92100
embeddings:
@@ -144,9 +152,9 @@
144152
---
145153
"Sparse vector in 7.x":
146154
- skip:
147-
features: allowed_warnings
148-
version: "all"
149-
reason: "AwaitsFix https://github.com/elastic/elasticsearch/issues/100003"
155+
features: ["allowed_warnings"]
156+
version: "8.0.0 - "
157+
reason: "sparse_vector field type supported in 7.x"
150158
- do:
151159
allowed_warnings:
152160
- "The [sparse_vector] field type is deprecated and will be removed in 8.0."
@@ -164,17 +172,16 @@
164172
- match: { acknowledged: true }
165173

166174
- do:
167-
catch: /\[sparse_vector\] fields do not support \[exists\] queries/
175+
allowed_warnings:
176+
- "[sparse_vector] field type in old 7.x indices is allowed to contain [sparse_vector] fields, but they cannot be indexed or searched."
168177
search:
169-
rest_total_hits_as_int: true
170178
index: test
171179
body:
172180
query:
173181
exists:
174182
field: ml.tokens
175-
176183
---
177-
"Sparse vector in 8.x":
184+
"Sparse vector in 8.0.0 <= x < 8.11.0":
178185
- skip:
179186
version: " - 7.99.99, 8.11.0 - "
180187
reason: "sparse_vector field type not supported in 8.x until 8.11.0"
@@ -189,3 +196,12 @@
189196
type: text
190197
ml.tokens:
191198
type: sparse_vector
199+
- do:
200+
catch: /\[sparse_vector\] fields do not support \[exists\] queries|no such index.*/
201+
search:
202+
rest_total_hits_as_int: true
203+
index: test
204+
body:
205+
query:
206+
exists:
207+
field: ml.tokens

server/src/main/java/org/elasticsearch/index/mapper/vectors/SparseVectorFieldMapper.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package org.elasticsearch.index.mapper.vectors;
1010

1111
import org.apache.lucene.document.FeatureField;
12+
import org.apache.lucene.search.MatchNoDocsQuery;
1213
import org.apache.lucene.search.Query;
1314
import org.apache.lucene.util.BytesRef;
1415
import org.elasticsearch.common.logging.DeprecationCategory;
@@ -110,8 +111,11 @@ public Query termQuery(Object value, SearchExecutionContext context) {
110111

111112
@Override
112113
public Query existsQuery(SearchExecutionContext context) {
113-
// No support for exists queries prior to this version
114-
if (context.getIndexSettings().getIndexVersionCreated().before(SPARSE_VECTOR_IN_FIELD_NAMES_INDEX_VERSION)) {
114+
if (context.getIndexSettings().getIndexVersionCreated().before(PREVIOUS_SPARSE_VECTOR_INDEX_VERSION)) {
115+
deprecationLogger.warn(DeprecationCategory.MAPPINGS, "sparse_vector", ERROR_MESSAGE_7X);
116+
return new MatchNoDocsQuery();
117+
} else if (context.getIndexSettings().getIndexVersionCreated().before(SPARSE_VECTOR_IN_FIELD_NAMES_INDEX_VERSION)) {
118+
// No support for exists queries prior to this version on 8.x
115119
throw new IllegalArgumentException("[sparse_vector] fields do not support [exists] queries");
116120
}
117121
return super.existsQuery(context);

0 commit comments

Comments
 (0)