alessandrobenedetti commented on a change in pull request #476: URL: https://github.com/apache/solr/pull/476#discussion_r777413036
########## File path: solr/core/src/java/org/apache/solr/schema/DenseVectorField.java ########## @@ -0,0 +1,211 @@ +/* + * 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.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.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; + +/** + * 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). + * See subclasses for details. + * + * <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"; + + int dimension; + VectorSimilarityFunction similarityFunction; + VectorSimilarityFunction DEFAULT_SIMILARITY = VectorSimilarityFunction.EUCLIDEAN; + + @Override + public void init(IndexSchema schema, Map<String, String> args) { + this.dimension = ofNullable(args.get(KNN_VECTOR_DIMENSION)) + .map(value -> parseDimensionParameter(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.properties &= ~MULTIVALUED; + this.properties &= ~UNINVERTIBLE; + + super.init(schema, args); + } + + private int parseDimensionParameter(String value) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "the vector dimension must be an integer"); + } + } + + public int getDimension() { + return dimension; + } + + @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()); + } Review comment: Hi @fmmoret , there's no ticket yet but it's in our to-do list to make an analysis of the feature, investigate the state of the art, and design and implement a solution (it may be Lucene side and Solr side, or potentially only Solr side). We'll publish the Pull Request soon :) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
