Skip to content

Commit c53d73d

Browse files
authored
Fix knn search error when dimensions are not set (#131081) (#131118)
* 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 (cherry picked from commit bbc4cef) # Conflicts: # server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java # x-pack/plugin/esql/qa/testFixtures/src/main/resources/knn-function.csv-spec
1 parent f1a5935 commit c53d73d

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
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
@@ -31,6 +31,7 @@
3131
import org.apache.lucene.index.VectorEncoding;
3232
import org.apache.lucene.index.VectorSimilarityFunction;
3333
import org.apache.lucene.search.FieldExistsQuery;
34+
import org.apache.lucene.search.MatchNoDocsQuery;
3435
import org.apache.lucene.search.Query;
3536
import org.apache.lucene.search.join.BitSetProducer;
3637
import org.apache.lucene.search.knn.KnnSearchStrategy;
@@ -2446,6 +2447,9 @@ public Query createKnnQuery(
24462447
"to perform knn search on field [" + name() + "], its mapping must have [index] set to [true]"
24472448
);
24482449
}
2450+
if (dims == null) {
2451+
return new MatchNoDocsQuery("No data has been indexed for field [" + name() + "]");
2452+
}
24492453
KnnSearchStrategy knnSearchStrategy = heuristic.getKnnSearchStrategy();
24502454
return switch (getElementType()) {
24512455
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
}

0 commit comments

Comments
 (0)