Pulkitg64 commented on code in PR #16383: URL: https://github.com/apache/lucene/pull/16383#discussion_r3582094032
########## lucene/core/src/java/org/apache/lucene/document/KnnFloat16VectorField.java: ########## @@ -0,0 +1,183 @@ +/* + * 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.lucene.document; + +import java.util.Objects; +import org.apache.lucene.index.Float16VectorValues; +import org.apache.lucene.index.VectorEncoding; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.search.KnnFloat16VectorQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.util.VectorUtil; + +/** + * A field that contains a single float16 numeric vector (or none) for each document. Vectors are + * dense - that is, every dimension of a vector contains an explicit value, stored packed into an + * array (of type short[]) whose length is the vector dimension. Values can be retrieved using + * {@link Float16VectorValues}, which is a forward-only docID-based iterator and also offers + * random-access by dense ordinal (not docId). {@link VectorSimilarityFunction} may be used to + * compare vectors at query time (for example as part of result ranking). A {@link + * KnnFloat16VectorField} may be associated with a search similarity function defining the metric + * used for nearest-neighbor search among vectors of that field. + * + * @lucene.experimental + */ +public class KnnFloat16VectorField extends Field { + + private static FieldType createType(short[] v, VectorSimilarityFunction similarityFunction) { + if (v == null) { + throw new IllegalArgumentException("vector value must not be null"); + } + int dimension = v.length; + if (dimension == 0) { + throw new IllegalArgumentException("cannot index an empty vector"); + } + if (similarityFunction == null) { + throw new IllegalArgumentException("similarity function must not be null"); + } + FieldType type = new FieldType(); + type.setVectorAttributes(dimension, VectorEncoding.FLOAT16, similarityFunction); + type.freeze(); + return type; + } + + /** + * A convenience method for creating a vector field type. + * + * @param dimension dimension of vectors + * @param similarityFunction a function defining vector proximity. + * @throws IllegalArgumentException if any parameter is null, or has dimension > 1024. + */ + public static FieldType createFieldType( + int dimension, VectorSimilarityFunction similarityFunction) { + FieldType type = new FieldType(); + type.setVectorAttributes(dimension, VectorEncoding.FLOAT16, similarityFunction); + type.freeze(); + return type; + } + + /** + * Create a new vector query for the provided field targeting the float vector + * + * @param field The field to query + * @param queryVector The float vector target + * @param k The number of nearest neighbors to gather + * @return A new vector query + */ + public static Query newVectorQuery(String field, short[] queryVector, int k) { + return new KnnFloat16VectorQuery(field, queryVector, k); + } + + /** + * Creates a numeric vector field. Fields are single-valued: each document has either one value or + * no value. Vectors of a single field share the same dimension and similarity function. Note that + * some vector similarities (like {@link VectorSimilarityFunction#DOT_PRODUCT}) require values to + * be unit-length, which can be enforced using {@link VectorUtil#l2normalize(float[])}. + * + * @param name field name + * @param vector value + * @param similarityFunction a function defining vector proximity. + * @throws IllegalArgumentException if any parameter is null, or the vector is empty or has + * dimension > 1024. + */ + public KnnFloat16VectorField( + String name, short[] vector, VectorSimilarityFunction similarityFunction) { + super(name, createType(vector, similarityFunction)); + fieldsData = vector; // null check done above + } + + /** + * Creates a new KnnFloatVectorField with the specified name, vector, similarity function, and + * encoding. + * + * @param name the field name + * @param vector the float vector value + * @param similarityFunction the similarity function to use for vector comparisons + * @param vectorEncoding the encoding format for the vector + */ + public KnnFloat16VectorField( + String name, + short[] vector, + VectorSimilarityFunction similarityFunction, + VectorEncoding vectorEncoding) { Review Comment: Yeah, I already removed it in my next revision. Thanks for noticing it. -- 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]
