You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ka...@apache.org on 2026/10/01 20:14:12 UTC
(lucene) branch main updated: Add `DiversifyingChildrenFloat16KnnVectorQuery` for fp16 parent-block-join KNN search (#16738)
This is an automated email from the ASF dual-hosted git repository.
kaivalnp pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git
The following commit(s) were added to refs/heads/main by this push:
new cb276351c10 Add `DiversifyingChildrenFloat16KnnVectorQuery` for fp16 parent-block-join KNN search (#16738)
cb276351c10 is described below
commit cb276351c10966e8afb3e234e9147d6145038e6a
Author: Pulkit Gupta <36...@users.noreply.github.com>
AuthorDate: Thu Oct 1 16:14:06 2026 -0400
Add `DiversifyingChildrenFloat16KnnVectorQuery` for fp16 parent-block-join KNN search (#16738)
---
lucene/CHANGES.txt | 3 +
.../search/Float16VectorSimilarityQuery.java | 4 +-
.../DiversifyingChildrenByteKnnVectorQuery.java | 36 +-----
...DiversifyingChildrenFloat16KnnVectorQuery.java} | 79 ++++---------
.../DiversifyingChildrenFloatKnnVectorQuery.java | 85 +-------------
.../join/DiversifyingChildrenVectorScorer.java | 126 +++++++++++++++++++++
.../TestParentBlockJoinByteKnnVectorQuery.java | 9 ++
... TestParentBlockJoinFloat16KnnVectorQuery.java} | 56 +++++----
.../TestParentBlockJoinFloatKnnVectorQuery.java | 9 ++
9 files changed, 211 insertions(+), 196 deletions(-)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 3a66cee3964..151a044cb13 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -151,6 +151,9 @@ New Features
* GITHUB#16735: Add Float16VectorSimilarityQuery for similarity threshold search over fp16 vectors.
(Recep Eken)
+* GITHUB#16738: Add DiversifyingChildrenFloat16KnnVectorQuery for fp16 parent-block-join KNN
+ search. (Pulkit Gupta)
+
Improvements
---------------------
* GITHUB#15704: Replace LinkedList with more efficient data structure. (Renato Haeberli)
diff --git a/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityQuery.java b/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityQuery.java
index 79775c7bf3b..46a190fee1b 100644
--- a/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityQuery.java
@@ -112,9 +112,9 @@ public abstract sealed class Float16VectorSimilarityQuery extends AbstractVector
public String toString(String field) {
return String.format(
Locale.ROOT,
- "Float16VectorSimilarityQuery.Adaptive[field=%s target=[%f...] resultSimilarity=%f decay=%f filter=%s]",
+ "Float16VectorSimilarityQuery.Adaptive[field=%s target=[%d...] resultSimilarity=%f decay=%f filter=%s]",
field,
- Float.float16ToFloat(super.target[0]),
+ super.target[0],
resultSimilarity,
decay,
filter);
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java
index 877d004d5c7..83daf40a65f 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java
@@ -26,15 +26,12 @@ import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.search.HitQueue;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.KnnByteVectorQuery;
import org.apache.lucene.search.KnnCollector;
import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopDocsCollector;
-import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.search.knn.KnnCollectorManager;
import org.apache.lucene.search.knn.KnnSearchStrategy;
@@ -121,37 +118,8 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
if (scorer == null) {
return NO_RESULTS;
}
- DiversifyingChildrenFloatKnnVectorQuery.DiversifyingChildrenVectorScorer vectorScorer =
- new DiversifyingChildrenFloatKnnVectorQuery.DiversifyingChildrenVectorScorer(
- acceptIterator, parentBitSet, scorer);
- final int queueSize = Math.min(k, Math.toIntExact(acceptIterator.cost()));
- HitQueue queue = new HitQueue(queueSize, true);
- TotalHits.Relation relation = TotalHits.Relation.EQUAL_TO;
- ScoreDoc topDoc = queue.top();
- while (vectorScorer.nextParent() != DocIdSetIterator.NO_MORE_DOCS) {
- // Mark results as partial if timeout is met
- if (queryTimeout != null && queryTimeout.shouldExit()) {
- relation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
- break;
- }
-
- float score = vectorScorer.score();
- if (score > topDoc.score) {
- topDoc.score = score;
- topDoc.doc = vectorScorer.bestChild();
- topDoc = queue.updateTop();
- }
- }
-
- // Remove any remaining sentinel values
- while (queue.size() > 0 && queue.top().score < 0) {
- queue.pop();
- }
-
- ScoreDoc[] topScoreDocs = queue.drainToArrayHighestFirst(ScoreDoc[]::new);
-
- TotalHits totalHits = new TotalHits(acceptIterator.cost(), relation);
- return new TopDocs(totalHits, topScoreDocs);
+ return new DiversifyingChildrenVectorScorer(acceptIterator, parentBitSet, scorer)
+ .collect(k, queryTimeout);
}
@Override
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloat16KnnVectorQuery.java
similarity index 66%
copy from lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java
copy to lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloat16KnnVectorQuery.java
index 877d004d5c7..a20b5590d1f 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenByteKnnVectorQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloat16KnnVectorQuery.java
@@ -21,48 +21,45 @@ import static org.apache.lucene.search.knn.KnnSearchStrategy.Hnsw.DEFAULT;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
-import org.apache.lucene.index.ByteVectorValues;
+import org.apache.lucene.index.Float16VectorValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.search.HitQueue;
import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.KnnByteVectorQuery;
import org.apache.lucene.search.KnnCollector;
+import org.apache.lucene.search.KnnFloat16VectorQuery;
import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopDocsCollector;
-import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.search.knn.KnnCollectorManager;
import org.apache.lucene.search.knn.KnnSearchStrategy;
import org.apache.lucene.util.BitSet;
/**
- * kNN byte vector query that joins matching children vector documents with their parent doc id. The
- * top documents returned are the child document ids and the calculated scores. Here is how to use
- * this in conjunction with {@link ToParentBlockJoinQuery}.
+ * kNN float16 vector query that joins matching children vector documents with their parent doc id.
+ * The top documents returned are the child document ids and the calculated scores. Here is how to
+ * use this in conjunction with {@link ToParentBlockJoinQuery}.
*
* <pre><code class="language-java">
- * Query knnQuery = new DiversifyingChildrenByteKnnVectorQuery(fieldName, queryVector, ...);
+ * Query knnQuery = new DiversifyingChildrenFloat16KnnVectorQuery(fieldName, queryVector, ...);
* // Rewrite executes kNN search and collects nearest children docIds and their scores
* Query rewrittenKnnQuery = searcher.rewrite(knnQuery);
* // Join the scored children docs with their parents and score the parents
* Query childrenToParents = new ToParentBlockJoinQuery(rewrittenKnnQuery, parentsFilter, ScoreMode.MAX);
* </code></pre>
*/
-public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
+public class DiversifyingChildrenFloat16KnnVectorQuery extends KnnFloat16VectorQuery {
private static final TopDocs NO_RESULTS = TopDocsCollector.EMPTY_TOPDOCS;
private final BitSetProducer parentsFilter;
private final Query childFilter;
private final int k;
- private final byte[] query;
+ private final short[] query;
/**
- * Create a ToParentBlockJoinByteVectorQuery.
+ * Create a DiversifyingChildrenFloat16KnnVectorQuery.
*
* @param field the query field
* @param query the vector query
@@ -70,13 +67,13 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
* @param k how many parent documents to return given the matching children
* @param parentsFilter Filter identifying the parent documents.
*/
- public DiversifyingChildrenByteKnnVectorQuery(
- String field, byte[] query, Query childFilter, int k, BitSetProducer parentsFilter) {
+ public DiversifyingChildrenFloat16KnnVectorQuery(
+ String field, short[] query, Query childFilter, int k, BitSetProducer parentsFilter) {
this(field, query, childFilter, k, parentsFilter, DEFAULT);
}
/**
- * Create a DiversifyingChildrenByteKnnVectorQuery.
+ * Create a DiversifyingChildrenFloat16KnnVectorQuery.
*
* @param field the query field
* @param query the vector query
@@ -88,9 +85,9 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
* strategy.
* @lucene.experimental
*/
- public DiversifyingChildrenByteKnnVectorQuery(
+ public DiversifyingChildrenFloat16KnnVectorQuery(
String field,
- byte[] query,
+ short[] query,
Query childFilter,
int k,
BitSetProducer parentsFilter,
@@ -106,9 +103,9 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
protected TopDocs exactSearch(
LeafReaderContext context, DocIdSetIterator acceptIterator, QueryTimeout queryTimeout)
throws IOException {
- ByteVectorValues byteVectorValues = context.reader().getByteVectorValues(field);
- if (byteVectorValues == null) {
- ByteVectorValues.checkField(context.reader(), field);
+ Float16VectorValues float16VectorValues = context.reader().getFloat16VectorValues(field);
+ if (float16VectorValues == null) {
+ Float16VectorValues.checkField(context.reader(), field);
return NO_RESULTS;
}
@@ -116,42 +113,12 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
if (parentBitSet == null) {
return NO_RESULTS;
}
-
- VectorScorer scorer = byteVectorValues.scorer(query);
- if (scorer == null) {
+ VectorScorer float16VectorScorer = float16VectorValues.scorer(query);
+ if (float16VectorScorer == null) {
return NO_RESULTS;
}
- DiversifyingChildrenFloatKnnVectorQuery.DiversifyingChildrenVectorScorer vectorScorer =
- new DiversifyingChildrenFloatKnnVectorQuery.DiversifyingChildrenVectorScorer(
- acceptIterator, parentBitSet, scorer);
- final int queueSize = Math.min(k, Math.toIntExact(acceptIterator.cost()));
- HitQueue queue = new HitQueue(queueSize, true);
- TotalHits.Relation relation = TotalHits.Relation.EQUAL_TO;
- ScoreDoc topDoc = queue.top();
- while (vectorScorer.nextParent() != DocIdSetIterator.NO_MORE_DOCS) {
- // Mark results as partial if timeout is met
- if (queryTimeout != null && queryTimeout.shouldExit()) {
- relation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
- break;
- }
-
- float score = vectorScorer.score();
- if (score > topDoc.score) {
- topDoc.score = score;
- topDoc.doc = vectorScorer.bestChild();
- topDoc = queue.updateTop();
- }
- }
-
- // Remove any remaining sentinel values
- while (queue.size() > 0 && queue.top().score < 0) {
- queue.pop();
- }
-
- ScoreDoc[] topScoreDocs = queue.drainToArrayHighestFirst(ScoreDoc[]::new);
-
- TotalHits totalHits = new TotalHits(acceptIterator.cost(), relation);
- return new TopDocs(totalHits, topScoreDocs);
+ return new DiversifyingChildrenVectorScorer(acceptIterator, parentBitSet, float16VectorScorer)
+ .collect(k, queryTimeout);
}
@Override
@@ -166,7 +133,7 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
int visitedLimit,
KnnCollectorManager knnCollectorManager)
throws IOException {
- ByteVectorValues.checkField(context.reader(), field);
+ Float16VectorValues.checkField(context.reader(), field);
KnnCollector collector =
knnCollectorManager.newCollector(visitedLimit, searchStrategy, context);
if (collector == null) {
@@ -193,7 +160,7 @@ public class DiversifyingChildrenByteKnnVectorQuery extends KnnByteVectorQuery {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
- DiversifyingChildrenByteKnnVectorQuery that = (DiversifyingChildrenByteKnnVectorQuery) o;
+ DiversifyingChildrenFloat16KnnVectorQuery that = (DiversifyingChildrenFloat16KnnVectorQuery) o;
return k == that.k
&& Objects.equals(parentsFilter, that.parentsFilter)
&& Objects.equals(childFilter, that.childFilter)
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloatKnnVectorQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloatKnnVectorQuery.java
index e2d6179ec42..964e2f39760 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloatKnnVectorQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenFloatKnnVectorQuery.java
@@ -26,15 +26,12 @@ import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.search.AcceptDocs;
import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.search.HitQueue;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.KnnCollector;
import org.apache.lucene.search.KnnFloatVectorQuery;
import org.apache.lucene.search.Query;
-import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopDocsCollector;
-import org.apache.lucene.search.TotalHits;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.search.knn.KnnCollectorManager;
import org.apache.lucene.search.knn.KnnSearchStrategy;
@@ -120,37 +117,8 @@ public class DiversifyingChildrenFloatKnnVectorQuery extends KnnFloatVectorQuery
if (floatVectorScorer == null) {
return NO_RESULTS;
}
-
- DiversifyingChildrenVectorScorer vectorScorer =
- new DiversifyingChildrenVectorScorer(acceptIterator, parentBitSet, floatVectorScorer);
- final int queueSize = Math.min(k, Math.toIntExact(acceptIterator.cost()));
- HitQueue queue = new HitQueue(queueSize, true);
- TotalHits.Relation relation = TotalHits.Relation.EQUAL_TO;
- ScoreDoc topDoc = queue.top();
- while (vectorScorer.nextParent() != DocIdSetIterator.NO_MORE_DOCS) {
- // Mark results as partial if timeout is met
- if (queryTimeout != null && queryTimeout.shouldExit()) {
- relation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
- break;
- }
-
- float score = vectorScorer.score();
- if (score > topDoc.score) {
- topDoc.score = score;
- topDoc.doc = vectorScorer.bestChild();
- topDoc = queue.updateTop();
- }
- }
-
- // Remove any remaining sentinel values
- while (queue.size() > 0 && queue.top().score < 0) {
- queue.pop();
- }
-
- ScoreDoc[] topScoreDocs = queue.drainToArrayHighestFirst(ScoreDoc[]::new);
-
- TotalHits totalHits = new TotalHits(acceptIterator.cost(), relation);
- return new TopDocs(totalHits, topScoreDocs);
+ return new DiversifyingChildrenVectorScorer(acceptIterator, parentBitSet, floatVectorScorer)
+ .collect(k, queryTimeout);
}
@Override
@@ -205,53 +173,4 @@ public class DiversifyingChildrenFloatKnnVectorQuery extends KnnFloatVectorQuery
result = 31 * result + Arrays.hashCode(query);
return result;
}
-
- static class DiversifyingChildrenVectorScorer {
- private final VectorScorer vectorScorer;
- private final DocIdSetIterator vectorIterator;
- private final DocIdSetIterator acceptedChildrenIterator;
- private final BitSet parentBitSet;
- private int currentParent = -1;
- private int bestChild = -1;
- private float currentScore = Float.NEGATIVE_INFINITY;
-
- protected DiversifyingChildrenVectorScorer(
- DocIdSetIterator acceptedChildrenIterator, BitSet parentBitSet, VectorScorer vectorScorer) {
- this.acceptedChildrenIterator = acceptedChildrenIterator;
- this.vectorScorer = vectorScorer;
- this.vectorIterator = vectorScorer.iterator();
- this.parentBitSet = parentBitSet;
- }
-
- public int bestChild() {
- return bestChild;
- }
-
- public int nextParent() throws IOException {
- int nextChild = acceptedChildrenIterator.docID();
- if (nextChild == -1) {
- nextChild = acceptedChildrenIterator.nextDoc();
- }
- if (nextChild == DocIdSetIterator.NO_MORE_DOCS) {
- currentParent = DocIdSetIterator.NO_MORE_DOCS;
- return currentParent;
- }
- currentScore = Float.NEGATIVE_INFINITY;
- currentParent = parentBitSet.nextSetBit(nextChild);
- do {
- vectorIterator.advance(nextChild);
- float score = vectorScorer.score();
- if (score > currentScore) {
- bestChild = nextChild;
- currentScore = score;
- }
- } while ((nextChild = acceptedChildrenIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS
- && nextChild < currentParent);
- return currentParent;
- }
-
- public float score() throws IOException {
- return currentScore;
- }
- }
}
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenVectorScorer.java b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenVectorScorer.java
new file mode 100644
index 00000000000..81a9429da69
--- /dev/null
+++ b/lucene/join/src/java/org/apache/lucene/search/join/DiversifyingChildrenVectorScorer.java
@@ -0,0 +1,126 @@
+/*
+ * 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.search.join;
+
+import java.io.IOException;
+import org.apache.lucene.index.QueryTimeout;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.HitQueue;
+import org.apache.lucene.search.ScoreDoc;
+import org.apache.lucene.search.TopDocs;
+import org.apache.lucene.search.TotalHits;
+import org.apache.lucene.search.VectorScorer;
+import org.apache.lucene.util.BitSet;
+
+/**
+ * Iterates the accepted child documents one parent at a time, tracking the best scoring child of
+ * each parent. Scoring is delegated to the given {@link VectorScorer}.
+ *
+ * @lucene.experimental
+ */
+class DiversifyingChildrenVectorScorer {
+ private final VectorScorer vectorScorer;
+ private final DocIdSetIterator vectorIterator;
+ private final DocIdSetIterator acceptedChildrenIterator;
+ private final BitSet parentBitSet;
+ private int currentParent = -1;
+ private int bestChild = -1;
+ private float currentScore = Float.NEGATIVE_INFINITY;
+
+ /**
+ * @param acceptedChildrenIterator the child documents to score
+ * @param parentBitSet the parent documents
+ * @param vectorScorer scores a child document against the query vector
+ */
+ DiversifyingChildrenVectorScorer(
+ DocIdSetIterator acceptedChildrenIterator, BitSet parentBitSet, VectorScorer vectorScorer) {
+ this.acceptedChildrenIterator = acceptedChildrenIterator;
+ this.vectorScorer = vectorScorer;
+ this.vectorIterator = vectorScorer.iterator();
+ this.parentBitSet = parentBitSet;
+ }
+
+ private int bestChild() {
+ return bestChild;
+ }
+
+ private int nextParent() throws IOException {
+ int nextChild = acceptedChildrenIterator.docID();
+ if (nextChild == -1) {
+ nextChild = acceptedChildrenIterator.nextDoc();
+ }
+ if (nextChild == DocIdSetIterator.NO_MORE_DOCS) {
+ currentParent = DocIdSetIterator.NO_MORE_DOCS;
+ return currentParent;
+ }
+ currentScore = Float.NEGATIVE_INFINITY;
+ currentParent = parentBitSet.nextSetBit(nextChild);
+ do {
+ vectorIterator.advance(nextChild);
+ float score = vectorScorer.score();
+ if (score > currentScore) {
+ bestChild = nextChild;
+ currentScore = score;
+ }
+ } while ((nextChild = acceptedChildrenIterator.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS
+ && nextChild < currentParent);
+ return currentParent;
+ }
+
+ private float score() throws IOException {
+ return currentScore;
+ }
+
+ /**
+ * Returns the top {@code k} scoring children, at most one per parent document. The results are
+ * marked as a lower bound if the given timeout is met before every parent has been visited. The
+ * accepted children are consumed, so this may be called once per instance.
+ *
+ * @param k how many children to return
+ * @param queryTimeout the timeout to honour, or null for no timeout
+ */
+ TopDocs collect(int k, QueryTimeout queryTimeout) throws IOException {
+ final int queueSize = Math.min(k, Math.toIntExact(acceptedChildrenIterator.cost()));
+ HitQueue queue = new HitQueue(queueSize, true);
+ TotalHits.Relation relation = TotalHits.Relation.EQUAL_TO;
+ ScoreDoc topDoc = queue.top();
+ while (nextParent() != DocIdSetIterator.NO_MORE_DOCS) {
+ // Mark results as partial if timeout is met
+ if (queryTimeout != null && queryTimeout.shouldExit()) {
+ relation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
+ break;
+ }
+
+ float score = score();
+ if (score > topDoc.score) {
+ topDoc.score = score;
+ topDoc.doc = bestChild();
+ topDoc = queue.updateTop();
+ }
+ }
+
+ // Remove any remaining sentinel values
+ while (queue.size() > 0 && queue.top().score < 0) {
+ queue.pop();
+ }
+
+ ScoreDoc[] topScoreDocs = queue.drainToArrayHighestFirst(ScoreDoc[]::new);
+
+ TotalHits totalHits = new TotalHits(acceptedChildrenIterator.cost(), relation);
+ return new TopDocs(totalHits, topScoreDocs);
+ }
+}
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinByteKnnVectorQuery.java b/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinByteKnnVectorQuery.java
index 4a15d1ec913..90cbbc321ff 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinByteKnnVectorQuery.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinByteKnnVectorQuery.java
@@ -81,6 +81,15 @@ public class TestParentBlockJoinByteKnnVectorQuery extends ParentBlockJoinKnnVec
new DiversifyingChildrenFloatKnnVectorQuery(
"field", new float[] {1, 2}, null, 2, parentFilter);
assertThrows(IllegalStateException.class, () -> searcher.search(kvq, 3));
+
+ Query float16Kvq =
+ new DiversifyingChildrenFloat16KnnVectorQuery(
+ "field",
+ new short[] {Float.floatToFloat16(1), Float.floatToFloat16(2)},
+ null,
+ 2,
+ parentFilter);
+ assertThrows(IllegalStateException.class, () -> searcher.search(float16Kvq, 3));
}
}
}
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java b/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloat16KnnVectorQuery.java
similarity index 81%
copy from lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java
copy to lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloat16KnnVectorQuery.java
index 9a5837b15db..4b450293845 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloat16KnnVectorQuery.java
@@ -25,7 +25,7 @@ import java.util.List;
import java.util.Random;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
-import org.apache.lucene.document.KnnFloatVectorField;
+import org.apache.lucene.document.KnnFloat16VectorField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
@@ -38,7 +38,8 @@ import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.util.TestUtil;
-public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVectorQueryTestCase {
+public class TestParentBlockJoinFloat16KnnVectorQuery
+ extends ParentBlockJoinKnnVectorQueryTestCase {
@Override
Query getParentJoinKnnQuery(
@@ -47,8 +48,19 @@ public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVe
Query childFilter,
int k,
BitSetProducer parentBitSet) {
- return new DiversifyingChildrenFloatKnnVectorQuery(
- fieldName, queryVector, childFilter, k, parentBitSet);
+ return new DiversifyingChildrenFloat16KnnVectorQuery(
+ fieldName, fromFloat(queryVector), childFilter, k, parentBitSet);
+ }
+
+ @Override
+ Field getKnnVectorField(String name, float[] vector) {
+ return new KnnFloat16VectorField(name, fromFloat(vector), VectorSimilarityFunction.EUCLIDEAN);
+ }
+
+ @Override
+ Field getKnnVectorField(
+ String name, float[] vector, VectorSimilarityFunction vectorSimilarityFunction) {
+ return new KnnFloat16VectorField(name, fromFloat(vector), vectorSimilarityFunction);
}
public void testVectorEncodingMismatch() throws IOException {
@@ -67,9 +79,14 @@ public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVe
IndexSearcher searcher = newSearcher(reader);
BitSetProducer parentFilter = parentFilter(reader);
Query kvq =
+ new DiversifyingChildrenFloatKnnVectorQuery(
+ "field", new float[] {1, 2}, null, 2, parentFilter);
+ assertThrows(IllegalStateException.class, () -> searcher.search(kvq, 3));
+
+ Query byteKvq =
new DiversifyingChildrenByteKnnVectorQuery(
"field", new byte[] {1, 2}, null, 2, parentFilter);
- assertThrows(IllegalStateException.class, () -> searcher.search(kvq, 3));
+ assertThrows(IllegalStateException.class, () -> searcher.search(byteKvq, 3));
}
}
}
@@ -96,9 +113,9 @@ public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVe
assertEquals(1, reader.leaves().size());
IndexSearcher searcher = new IndexSearcher(reader);
BitSetProducer parentFilter = parentFilter(searcher.getIndexReader());
- DiversifyingChildrenFloatKnnVectorQuery query =
- new DiversifyingChildrenFloatKnnVectorQuery(
- "field", new float[] {2, 3}, null, 3, parentFilter);
+ DiversifyingChildrenFloat16KnnVectorQuery query =
+ new DiversifyingChildrenFloat16KnnVectorQuery(
+ "field", fromFloat(new float[] {2, 3}), null, 3, parentFilter);
/* score0 = ((2,3) * (1, 1) = 5) / (||2, 3|| * ||1, 1|| = sqrt(26)), then
* normalized by (1 + x) /2.
*/
@@ -121,27 +138,16 @@ public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVe
// test without filter
Query query = getParentJoinKnnQuery("field", new float[] {0, 1}, null, 10, null);
assertEquals(
- "DiversifyingChildrenFloatKnnVectorQuery:field[0.0,...][10]", query.toString("ignored"));
+ "DiversifyingChildrenFloat16KnnVectorQuery:field[0,...][10]", query.toString("ignored"));
// test with filter
Query filter = new TermQuery(new Term("id", "text"));
query = getParentJoinKnnQuery("field", new float[] {0.0f, 1.0f}, filter, 10, null);
assertEquals(
- "DiversifyingChildrenFloatKnnVectorQuery:field[0.0,...][10][id:text]",
+ "DiversifyingChildrenFloat16KnnVectorQuery:field[0,...][10][id:text]",
query.toString("ignored"));
}
- @Override
- Field getKnnVectorField(String name, float[] vector) {
- return new KnnFloatVectorField(name, vector);
- }
-
- @Override
- Field getKnnVectorField(
- String name, float[] vector, VectorSimilarityFunction vectorSimilarityFunction) {
- return new KnnFloatVectorField(name, vector, vectorSimilarityFunction);
- }
-
@Override
float[] randomVector(int dim) {
float[] v = new float[dim];
@@ -151,4 +157,12 @@ public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVe
}
return v;
}
+
+ private static short[] fromFloat(float[] queryVector) {
+ short[] query = new short[queryVector.length];
+ for (int i = 0; i < queryVector.length; i++) {
+ query[i] = Float.floatToFloat16(queryVector[i]);
+ }
+ return query;
+ }
}
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java b/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java
index 9a5837b15db..991ce4f3141 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestParentBlockJoinFloatKnnVectorQuery.java
@@ -70,6 +70,15 @@ public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVe
new DiversifyingChildrenByteKnnVectorQuery(
"field", new byte[] {1, 2}, null, 2, parentFilter);
assertThrows(IllegalStateException.class, () -> searcher.search(kvq, 3));
+
+ Query float16Kvq =
+ new DiversifyingChildrenFloat16KnnVectorQuery(
+ "field",
+ new short[] {Float.floatToFloat16(1), Float.floatToFloat16(2)},
+ null,
+ 2,
+ parentFilter);
+ assertThrows(IllegalStateException.class, () -> searcher.search(float16Kvq, 3));
}
}
}