Skip to content

Commit bbc4cef

Browse files
authored
Fix knn search error when dimensions are not set (#131081)
* Unmute tests muted in #129550 as they were fixed in #127322 * Return no match when no dimensions have been set * Add tests * Update docs/changelog/131081.yaml
1 parent 285866e commit bbc4cef

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

docs/changelog/131081.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 131081
2+
summary: Fix knn search error when dimensions are not set
3+
area: Vector Search
4+
type: bug
5+
issues:
6+
- 129550

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.vectors/40_knn_search.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,36 @@ setup:
670670
properties:
671671
embedding:
672672
type: dense_vector
673+
674+
675+
---
676+
"Searching with no data dimensions specified":
677+
- requires:
678+
cluster_features: "search.vectors.no_dimensions_bugfix"
679+
reason: "Search with no dimensions bugfix"
680+
681+
- do:
682+
indices.create:
683+
index: empty-test
684+
body:
685+
mappings:
686+
properties:
687+
vector:
688+
type: dense_vector
689+
index: true
690+
691+
- do:
692+
search:
693+
index: empty-test
694+
body:
695+
fields: [ "name" ]
696+
knn:
697+
field: vector
698+
query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
699+
k: 3
700+
num_candidates: 3
701+
rescore_vector:
702+
oversample: 1.5
703+
similarity: 0.1
704+
705+
- match: { hits.total.value: 0 }

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.lucene.search.FieldExistsQuery;
3636
import org.apache.lucene.search.KnnByteVectorQuery;
3737
import org.apache.lucene.search.KnnFloatVectorQuery;
38+
import org.apache.lucene.search.MatchNoDocsQuery;
3839
import org.apache.lucene.search.PatienceKnnVectorQuery;
3940
import org.apache.lucene.search.Query;
4041
import org.apache.lucene.search.join.BitSetProducer;
@@ -2530,6 +2531,9 @@ public Query createKnnQuery(
25302531
"to perform knn search on field [" + name() + "], its mapping must have [index] set to [true]"
25312532
);
25322533
}
2534+
if (dims == null) {
2535+
return new MatchNoDocsQuery("No data has been indexed for field [" + name() + "]");
2536+
}
25332537
KnnSearchStrategy knnSearchStrategy = heuristic.getKnnSearchStrategy();
25342538
return switch (getElementType()) {
25352539
case BYTE -> createKnnByteQuery(

server/src/main/java/org/elasticsearch/search/SearchFeatures.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public Set<NodeFeature> getFeatures() {
3232
public static final NodeFeature INT_SORT_FOR_INT_SHORT_BYTE_FIELDS = new NodeFeature("search.sort.int_sort_for_int_short_byte_fields");
3333
static final NodeFeature MULTI_MATCH_CHECKS_POSITIONS = new NodeFeature("search.multi.match.checks.positions");
3434
public static final NodeFeature BBQ_HNSW_DEFAULT_INDEXING = new NodeFeature("search.vectors.mappers.default_bbq_hnsw");
35+
public static final NodeFeature SEARCH_WITH_NO_DIMENSIONS_BUGFIX = new NodeFeature("search.vectors.no_dimensions_bugfix");
3536

3637
@Override
3738
public Set<NodeFeature> getTestFeatures() {
@@ -41,7 +42,8 @@ public Set<NodeFeature> getTestFeatures() {
4142
RESCORER_MISSING_FIELD_BAD_REQUEST,
4243
INT_SORT_FOR_INT_SHORT_BYTE_FIELDS,
4344
MULTI_MATCH_CHECKS_POSITIONS,
44-
BBQ_HNSW_DEFAULT_INDEXING
45+
BBQ_HNSW_DEFAULT_INDEXING,
46+
SEARCH_WITH_NO_DIMENSIONS_BUGFIX
4547
);
4648
}
4749
}

x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ chartreuse | [127.0, 255.0, 0.0]
2929
// end::knn-function-result[]
3030
;
3131

32-
# https://github.com/elastic/elasticsearch/issues/129550 - Add as an example to knn function documentation
33-
knnSearchWithSimilarityOption-Ignore
32+
knnSearchWithSimilarityOption
3433
required_capability: knn_function_v2
3534

3635
from colors metadata _score
@@ -208,8 +207,7 @@ green | false
208207
maroon | false
209208
;
210209

211-
# https://github.com/elastic/elasticsearch/issues/129550
212-
testKnnWithNonPushableDisjunctions-Ignore
210+
testKnnWithNonPushableDisjunctions
213211
required_capability: knn_function_v2
214212

215213
from colors metadata _score
@@ -225,8 +223,7 @@ lemon chiffon
225223
papaya whip
226224
;
227225

228-
# https://github.com/elastic/elasticsearch/issues/129550
229-
testKnnWithNonPushableDisjunctionsOnComplexExpressions-Ignore
226+
testKnnWithNonPushableDisjunctionsOnComplexExpressions
230227
required_capability: knn_function_v2
231228

232229
from colors metadata _score

0 commit comments

Comments
 (0)