alessandrobenedetti commented on a change in pull request #476:
URL: https://github.com/apache/solr/pull/476#discussion_r786728212



##########
File path: solr/core/src/java/org/apache/solr/schema/DenseVectorField.java
##########
@@ -0,0 +1,256 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.schema;
+
+import org.apache.lucene.codecs.lucene90.Lucene90HnswVectorsFormat;
+import org.apache.lucene.document.KnnVectorField;
+import org.apache.lucene.index.IndexableField;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.queries.function.ValueSource;
+import org.apache.lucene.search.KnnVectorQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.util.hnsw.HnswGraph;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.search.QParser;
+import org.apache.solr.uninverting.UninvertingReader;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import static java.util.Optional.ofNullable;
+import static 
org.apache.lucene.codecs.lucene90.Lucene90HnswVectorsFormat.DEFAULT_BEAM_WIDTH;
+import static 
org.apache.lucene.codecs.lucene90.Lucene90HnswVectorsFormat.DEFAULT_MAX_CONN;
+
+/**
+ * Provides a field type to support Lucene's {@link
+ * org.apache.lucene.document.KnnVectorField}.
+ * See {@link org.apache.lucene.search.KnnVectorQuery} for more details.
+ * It supports a fixed cardinality dimension for the vector and a fixed 
similarity function.
+ * The default similarity is EUCLIDEAN_HNSW (L2).
+ * The default index codec format is specified in the Lucene Codec constructor.
+ * For Lucene 9.0 e.g.
+ * See {@link org.apache.lucene.codecs.lucene90.Lucene90Codec}
+ * Currently only {@link 
org.apache.lucene.codecs.lucene90.Lucene90HnswVectorsFormat} is supported for
+ * advanced hyper-parameter customisation.
+ * See {@link org.apache.lucene.util.hnsw.HnswGraph} for more details about 
the implementation. 
+ *
+ * <br>
+ * Only {@code Indexed} and {@code Stored} attributes are supported.
+ */
+public class DenseVectorField extends FloatPointField {
+
+    static final String KNN_VECTOR_DIMENSION = "vectorDimension";
+    static final String KNN_SIMILARITY_FUNCTION = "similarityFunction";
+    
+    static final String CODEC_FORMAT = "codecFormat";
+    static final String HNSW_MAX_CONNECTIONS = "hnswMaxConnections";
+    static final String HNSW_BEAM_WIDTH = "hnswBeamWidth";
+
+    int dimension;
+    VectorSimilarityFunction similarityFunction;
+    VectorSimilarityFunction DEFAULT_SIMILARITY = 
VectorSimilarityFunction.EUCLIDEAN;
+
+    String codecFormat;
+    /**
+     * This parameter is coupled with the {@link Lucene90HnswVectorsFormat} 
format implementation.
+     * Controls how many of the nearest neighbor candidates are connected to 
the new node. Defaults to
+     * {@link Lucene90HnswVectorsFormat#DEFAULT_MAX_CONN}. See {@link 
HnswGraph} for more details.
+     */
+    int hnswMaxConn;
+    /**
+     * This parameter is coupled with the {@link Lucene90HnswVectorsFormat} 
format implementation.
+     * The number of candidate neighbors to track while searching the graph 
for each newly inserted
+     * node. Defaults to to {@link 
Lucene90HnswVectorsFormat#DEFAULT_BEAM_WIDTH}. See {@link
+     * HnswGraph} for details.
+     */
+    int hnswBeamWidth;
+
+    @Override
+    public void init(IndexSchema schema, Map<String, String> args) {
+        this.dimension = ofNullable(args.get(KNN_VECTOR_DIMENSION))
+                .map(value -> Integer.parseInt(value))
+                .orElseThrow(() -> new 
SolrException(SolrException.ErrorCode.SERVER_ERROR, "the vector dimension is a 
mandatory parameter"));
+        args.remove(KNN_VECTOR_DIMENSION);
+
+        this.similarityFunction = ofNullable(args.get(KNN_SIMILARITY_FUNCTION))
+                .map(value -> 
VectorSimilarityFunction.valueOf(value.toUpperCase(Locale.ROOT)))
+                .orElse(DEFAULT_SIMILARITY);
+        args.remove(KNN_SIMILARITY_FUNCTION);
+
+        this.codecFormat = args.get(CODEC_FORMAT);
+        args.remove(CODEC_FORMAT);
+
+        this.hnswMaxConn = ofNullable(args.get(HNSW_MAX_CONNECTIONS))
+                .map(value -> Integer.parseInt(value))
+                .orElse(DEFAULT_MAX_CONN);
+        args.remove(HNSW_MAX_CONNECTIONS);
+
+        this.hnswBeamWidth = ofNullable(args.get(HNSW_BEAM_WIDTH))
+                .map(value -> Integer.parseInt(value))
+                .orElse(DEFAULT_BEAM_WIDTH);
+        args.remove(HNSW_BEAM_WIDTH);
+
+        this.properties &= ~MULTIVALUED;
+        this.properties &= ~UNINVERTIBLE;
+        
+        super.init(schema, args);
+    }
+
+    public int getDimension() {
+        return dimension;
+    }
+
+    public String getCodecFormat() {
+        return codecFormat;
+    }
+
+    public Integer getHnswMaxConn() {
+        return hnswMaxConn;
+    }
+
+    public Integer getHnswBeamWidth() {
+        return hnswBeamWidth;
+    }
+
+    @Override
+    public void checkSchemaField(final SchemaField field) throws SolrException 
{
+        super.checkSchemaField(field);
+        if (field.multiValued()) {
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+                    getClass().getSimpleName() + " fields can not be 
multiValued: " + field.getName());
+        }
+
+        if (field.hasDocValues()) {
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+                    getClass().getSimpleName() + " fields can not have 
docValues: " + field.getName());
+        }
+    }
+    
+    public List<IndexableField> createFields(SchemaField field, Object value) {
+        List<IndexableField> fields = new ArrayList<>();
+        float[] parsedVector;
+        try {
+            parsedVector = parseVector(value);
+        } catch (RuntimeException e) {
+            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, 
"Error while creating field '" + field + "' from value '" + value + "', 
expected format:'[f1, f2, f3...fn]' e.g. [1.0, 3.4, 5.6]", e);
+        }
+

Review comment:
       That's a good idea to reduce the arraylist resizes!
   I've done with:
   
   if (field.stored()) {
               fields.ensureCapacity(parsedVector.length + 1);
               for (float vectorElement : parsedVector) {
                   fields.add(getStoredField(field, vectorElement));
               }
           }
           
           Thanks!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to