This is an automated email from the ASF dual-hosted git repository.

abenedetti pushed a commit to branch branch_9_1
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_9_1 by this push:
     new 39b79f834dd SOLR-16588: set default knn algorithm (#1255)
39b79f834dd is described below

commit 39b79f834ddbc1bca2380d3fb6182acc2a57c540
Author: Alessandro Benedetti <[email protected]>
AuthorDate: Mon Jan 9 12:33:21 2023 +0100

    SOLR-16588: set default knn algorithm (#1255)
    
    Co-authored-by: Elia <[email protected]>
    Co-authored-by: Kevin Risden <[email protected]>
---
 solr/CHANGES.txt                                           |  2 ++
 .../src/java/org/apache/solr/core/SchemaCodecFactory.java  | 14 +++++++-------
 .../src/java/org/apache/solr/schema/DenseVectorField.java  |  3 ++-
 .../test/org/apache/solr/schema/DenseVectorFieldTest.java  |  3 ++-
 4 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index a18397a8a1e..61764bfa6c2 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -26,6 +26,8 @@ Other Changes
 
 * SOLR-16567: Fixed problem with filtering and KNN search, especially when 
using post-filters (Alessandro Benedetti)
 
+* SOLR-16588: Fixed problem with default knn algorithm (Elia Porciani via 
Alessandro Benedetti)
+
 ==================  9.1.0 ==================
 
 New Features
diff --git a/solr/core/src/java/org/apache/solr/core/SchemaCodecFactory.java 
b/solr/core/src/java/org/apache/solr/core/SchemaCodecFactory.java
index 0b43275d617..41019ba6a59 100644
--- a/solr/core/src/java/org/apache/solr/core/SchemaCodecFactory.java
+++ b/solr/core/src/java/org/apache/solr/core/SchemaCodecFactory.java
@@ -124,18 +124,18 @@ public class SchemaCodecFactory extends CodecFactory 
implements SolrCoreAware {
             if (fieldType instanceof DenseVectorField) {
               DenseVectorField vectorType = (DenseVectorField) fieldType;
               String knnAlgorithm = vectorType.getKnnAlgorithm();
-              if (knnAlgorithm != null) {
-                if (knnAlgorithm.equals(DenseVectorField.HNSW_ALGORITHM)) {
-                  int maxConn = vectorType.getHnswMaxConn();
-                  int beamWidth = vectorType.getHnswBeamWidth();
-                  return new Lucene92HnswVectorsFormat(maxConn, beamWidth);
-                }
+              if (DenseVectorField.HNSW_ALGORITHM.equals(knnAlgorithm)) {
+                int maxConn = vectorType.getHnswMaxConn();
+                int beamWidth = vectorType.getHnswBeamWidth();
+                return new Lucene92HnswVectorsFormat(maxConn, beamWidth);
               } else {
                 throw new SolrException(
                     ErrorCode.SERVER_ERROR, knnAlgorithm + " KNN algorithm is 
not supported");
               }
             }
-            return super.getKnnVectorsFormatForField(field);
+
+            throw new SolrException(
+                ErrorCode.SERVER_ERROR, "wrong field type for KNN vectors: " + 
fieldType);
           }
         };
   }
diff --git a/solr/core/src/java/org/apache/solr/schema/DenseVectorField.java 
b/solr/core/src/java/org/apache/solr/schema/DenseVectorField.java
index 7210b8a4293..fddf06de4f9 100644
--- a/solr/core/src/java/org/apache/solr/schema/DenseVectorField.java
+++ b/solr/core/src/java/org/apache/solr/schema/DenseVectorField.java
@@ -47,6 +47,7 @@ import org.apache.solr.uninverting.UninvertingReader;
 public class DenseVectorField extends FloatPointField {
   public static final String HNSW_ALGORITHM = "hnsw";
 
+  public static final String DEFAULT_KNN_ALGORITHM = HNSW_ALGORITHM;
   static final String KNN_VECTOR_DIMENSION = "vectorDimension";
   static final String KNN_SIMILARITY_FUNCTION = "similarityFunction";
 
@@ -104,7 +105,7 @@ public class DenseVectorField extends FloatPointField {
             .orElse(DEFAULT_SIMILARITY);
     args.remove(KNN_SIMILARITY_FUNCTION);
 
-    this.knnAlgorithm = args.get(KNN_ALGORITHM);
+    this.knnAlgorithm = args.getOrDefault(KNN_ALGORITHM, 
DEFAULT_KNN_ALGORITHM);
 
     args.remove(KNN_ALGORITHM);
 
diff --git 
a/solr/core/src/test/org/apache/solr/schema/DenseVectorFieldTest.java 
b/solr/core/src/test/org/apache/solr/schema/DenseVectorFieldTest.java
index eb84ee237d1..ef64d14a67a 100644
--- a/solr/core/src/test/org/apache/solr/schema/DenseVectorFieldTest.java
+++ b/solr/core/src/test/org/apache/solr/schema/DenseVectorFieldTest.java
@@ -145,6 +145,7 @@ public class DenseVectorFieldTest extends 
AbstractBadConfigTestBase {
       DenseVectorField type3 = (DenseVectorField) vector3.getType();
       MatcherAssert.assertThat(type3.getSimilarityFunction(), 
is(VectorSimilarityFunction.COSINE));
       MatcherAssert.assertThat(type3.getDimension(), is(5));
+
       MatcherAssert.assertThat(type3.getKnnAlgorithm(), is("hnsw"));
       MatcherAssert.assertThat(type3.getHnswMaxConn(), is(8));
       MatcherAssert.assertThat(type3.getHnswBeamWidth(), is(46));
@@ -155,8 +156,8 @@ public class DenseVectorFieldTest extends 
AbstractBadConfigTestBase {
       DenseVectorField typeDefault = (DenseVectorField) 
vectorDefault.getType();
       MatcherAssert.assertThat(
           typeDefault.getSimilarityFunction(), 
is(VectorSimilarityFunction.COSINE));
+      MatcherAssert.assertThat(typeDefault.getKnnAlgorithm(), is("hnsw"));
       MatcherAssert.assertThat(typeDefault.getDimension(), is(4));
-      assertNull(typeDefault.getKnnAlgorithm());
       MatcherAssert.assertThat(typeDefault.getHnswMaxConn(), is(16));
       MatcherAssert.assertThat(typeDefault.getHnswBeamWidth(), is(100));
     } finally {

Reply via email to