vigyasharma commented on code in PR #13525: URL: https://github.com/apache/lucene/pull/13525#discussion_r1757395276
########## lucene/core/src/java/org/apache/lucene/index/MultiVectorSimilarityFunction.java: ########## @@ -0,0 +1,203 @@ +/* + * 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.index; + +import java.util.ArrayList; +import java.util.List; +import org.apache.lucene.util.ArrayUtil; + +/** + * Multi-vector similarity function; used in search to return top K most similar multi-vectors to a + * target multi-vector. This method is used during indexing and searching of the multi-vectors in + * order to determine the nearest neighbors. + */ +// no commit +public class MultiVectorSimilarityFunction implements MultiVectorSimilarity { + + /** Aggregation function to combine similarity across multiple vector values */ + public enum Aggregation { + /** Placeholder aggregation that is not intended to be used. */ + NONE { + @Override + public float aggregate( + float[] outer, + float[] inner, + VectorSimilarityFunction vectorSimilarityFunction, + int dimension) { + throw new UnsupportedOperationException(); + } + + @Override + public float aggregate( + byte[] outer, + byte[] inner, + VectorSimilarityFunction vectorSimilarityFunction, + int dimension) { + throw new UnsupportedOperationException(); + } + }, + + /** + * SumMaxSimilarity between two multi-vectors. Aggregates using the sum of maximum similarity + * found for each vector in the first multi-vector against all vectors in the second + * multi-vector. + */ + SUM_MAX { + @Override + public float aggregate( + float[] outer, + float[] inner, + VectorSimilarityFunction vectorSimilarityFunction, + int dimension) { + if (outer.length % dimension != 0 || inner.length % dimension != 0) { + throw new IllegalArgumentException("Multi vectors do not match provided dimensions"); + } + // TODO: can we avoid making vector copies? + List<float[]> outerList = new ArrayList<>(); + List<float[]> innerList = new ArrayList<>(); + for (int i = 0; i <= outer.length; i += dimension) { + outerList.add(ArrayUtil.copyOfSubArray(outer, i, dimension)); + } + for (int i = 0; i <= inner.length; i += dimension) { + innerList.add(ArrayUtil.copyOfSubArray(inner, i, dimension)); + } Review Comment: yes, that's what I had in mind. It'll be slower right now with all the extra allocation but we can change it later by making our sim. fn impl (probably in the vectorization provider?) work with offsets and lengths. -- 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...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org