benwtrent commented on code in PR #13525:
URL: https://github.com/apache/lucene/pull/13525#discussion_r1755216443


##########
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:
   Agreed, to do this, we will need to update our similarity functions to allow 
offsets & lengths. But it should be doable. Then, performance wise and logic 
wise `SUM_MAX` over two single vectors would be the same as going directly to 
the similarity function.
   



##########
lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java:
##########
@@ -77,6 +79,35 @@ public KnnFloatVectorQuery(String field, float[] target, int 
k, Query filter) {
     this.target = VectorUtil.checkFinite(Objects.requireNonNull(target, 
"target"));
   }
 
+  /**
+   * Convenience function to create a {@link KnnFloatVectorQuery} for 
multi-vector targets
+   *
+   * @param field a field that has been indexed as a {@link 
KnnFloatMultiVectorField}.
+   * @param target the target of the search
+   * @param k the number of documents to find
+   * @param filter a filter applied before the vector search
+   * @throws IllegalArgumentException if <code>k</code> is less than 1
+   * @return {@link KnnFloatVectorQuery}
+   */
+  public static KnnFloatVectorQuery create(
+      String field, List<float[]> target, int k, Query filter) {
+    if (target == null || target.isEmpty()) {
+      throw new IllegalArgumentException("empty target");
+    }
+    int targetDim = target.get(0).length;
+    float[] packedTarget = new float[targetDim * target.size()];

Review Comment:
   since we do this huge allocation, we should have some sane upper limit on 
the number of vectors allowed (maybe 10k? or 100k?).



##########
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:
   ^ this optimization can happen after this PR.



-- 
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

Reply via email to