瀏覽代碼

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
Carlos Delgado 3 月之前
父節點
當前提交
bbc4cefae3

+ 6 - 0
docs/changelog/131081.yaml

@@ -0,0 +1,6 @@
+pr: 131081
+summary: Fix knn search error when dimensions are not set
+area: Vector Search
+type: bug
+issues:
+ - 129550

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

@@ -670,3 +670,36 @@ setup:
           properties:
             embedding:
               type: dense_vector
+
+
+---
+"Searching with no data dimensions specified":
+  - requires:
+      cluster_features: "search.vectors.no_dimensions_bugfix"
+      reason: "Search with no dimensions bugfix"
+
+  - do:
+      indices.create:
+        index: empty-test
+        body:
+          mappings:
+            properties:
+              vector:
+                type: dense_vector
+                index: true
+
+  - do:
+      search:
+        index: empty-test
+        body:
+          fields: [ "name" ]
+          knn:
+            field: vector
+            query_vector: [ -0.5, 90.0, -10, 14.8, -156.0 ]
+            k: 3
+            num_candidates: 3
+            rescore_vector:
+              oversample: 1.5
+            similarity: 0.1
+
+  - match: { hits.total.value: 0 }

+ 4 - 0
server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

@@ -35,6 +35,7 @@ import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.FieldExistsQuery;
 import org.apache.lucene.search.KnnByteVectorQuery;
 import org.apache.lucene.search.KnnFloatVectorQuery;
+import org.apache.lucene.search.MatchNoDocsQuery;
 import org.apache.lucene.search.PatienceKnnVectorQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.join.BitSetProducer;
@@ -2530,6 +2531,9 @@ public class DenseVectorFieldMapper extends FieldMapper {
                     "to perform knn search on field [" + name() + "], its mapping must have [index] set to [true]"
                 );
             }
+            if (dims == null) {
+                return new MatchNoDocsQuery("No data has been indexed for field [" + name() + "]");
+            }
             KnnSearchStrategy knnSearchStrategy = heuristic.getKnnSearchStrategy();
             return switch (getElementType()) {
                 case BYTE -> createKnnByteQuery(

+ 3 - 1
server/src/main/java/org/elasticsearch/search/SearchFeatures.java

@@ -32,6 +32,7 @@ public final class SearchFeatures implements FeatureSpecification {
     public static final NodeFeature INT_SORT_FOR_INT_SHORT_BYTE_FIELDS = new NodeFeature("search.sort.int_sort_for_int_short_byte_fields");
     static final NodeFeature MULTI_MATCH_CHECKS_POSITIONS = new NodeFeature("search.multi.match.checks.positions");
     public static final NodeFeature BBQ_HNSW_DEFAULT_INDEXING = new NodeFeature("search.vectors.mappers.default_bbq_hnsw");
+    public static final NodeFeature SEARCH_WITH_NO_DIMENSIONS_BUGFIX = new NodeFeature("search.vectors.no_dimensions_bugfix");
 
     @Override
     public Set<NodeFeature> getTestFeatures() {
@@ -41,7 +42,8 @@ public final class SearchFeatures implements FeatureSpecification {
             RESCORER_MISSING_FIELD_BAD_REQUEST,
             INT_SORT_FOR_INT_SHORT_BYTE_FIELDS,
             MULTI_MATCH_CHECKS_POSITIONS,
-            BBQ_HNSW_DEFAULT_INDEXING
+            BBQ_HNSW_DEFAULT_INDEXING,
+            SEARCH_WITH_NO_DIMENSIONS_BUGFIX
         );
     }
 }

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

@@ -29,8 +29,7 @@ chartreuse | [127.0, 255.0, 0.0]
 // end::knn-function-result[]
 ;
 
-# https://github.com/elastic/elasticsearch/issues/129550 - Add as an example to knn function documentation
-knnSearchWithSimilarityOption-Ignore
+knnSearchWithSimilarityOption
 required_capability: knn_function_v2
 
 from colors metadata _score 
@@ -208,8 +207,7 @@ green      | false
 maroon     | false
 ;
 
-# https://github.com/elastic/elasticsearch/issues/129550
-testKnnWithNonPushableDisjunctions-Ignore
+testKnnWithNonPushableDisjunctions
 required_capability: knn_function_v2
 
 from colors metadata _score 
@@ -225,8 +223,7 @@ lemon chiffon
 papaya whip
 ;
 
-# https://github.com/elastic/elasticsearch/issues/129550
-testKnnWithNonPushableDisjunctionsOnComplexExpressions-Ignore
+testKnnWithNonPushableDisjunctionsOnComplexExpressions
 required_capability: knn_function_v2
 
 from colors metadata _score