You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by "benwtrent (via GitHub)" <gi...@apache.org> on 2023/11/14 19:27:59 UTC
[PR] Utilize exact kNN search when gathering k > numVectors in a segment [lucene]
benwtrent opened a new pull request, #12806:
URL: https://github.com/apache/lucene/pull/12806
When requesting for k >= numVectors, it doesn't make sense to go through the HNSW graph. Even without a user supplied filter, we should not explore the HNSW graph if it contains fewer than `k` vectors.
One scenario where we may still explore the graph if k >= numVectors is when not every document has a vector and there are deleted docs. But, this commit significantly improves things regardless.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Utilize exact kNN search when gathering k > numVectors in a segment [lucene]
Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on PR #12806:
URL: https://github.com/apache/lucene/pull/12806#issuecomment-1812421692
> The idea makes sense to me, what is less clear to me is whether this logic belongs to the Query or to the vector reader: should searchNearestNeighbors implicitly do a linear scan when k is greater than size()?
I like that idea, we should do that.
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Utilize exact kNN search when gathering k > numVectors in a segment [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #12806:
URL: https://github.com/apache/lucene/pull/12806#discussion_r1394090752
##########
lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java:
##########
@@ -110,6 +110,12 @@ private TopDocs getLeafResults(LeafReaderContext ctx, Weight filterWeight) throw
int maxDoc = ctx.reader().maxDoc();
if (filterWeight == null) {
+ int cost = liveDocs == null ? maxDoc : liveDocs.length();
Review Comment:
liveDocs.length() is always equal to `maxDoc`
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Utilize exact kNN search when gathering k > numVectors in a segment [lucene]
Posted by "jpountz (via GitHub)" <gi...@apache.org>.
jpountz commented on code in PR #12806:
URL: https://github.com/apache/lucene/pull/12806#discussion_r1394256922
##########
lucene/core/src/java/org/apache/lucene/codecs/lucene99/Lucene99HnswVectorsReader.java:
##########
@@ -238,11 +238,23 @@ public void search(String field, float[] target, KnnCollector knnCollector, Bits
return;
}
RandomVectorScorer scorer = flatVectorsReader.getRandomVectorScorer(field, target);
- HnswGraphSearcher.search(
- scorer,
- new OrdinalTranslatedKnnCollector(knnCollector, scorer::ordToDoc),
- getGraph(fieldEntry),
- scorer.getAcceptOrds(acceptDocs));
+ if (knnCollector.k() < scorer.maxOrd()) {
+ HnswGraphSearcher.search(
+ scorer,
+ new OrdinalTranslatedKnnCollector(knnCollector, scorer::ordToDoc),
+ getGraph(fieldEntry),
+ scorer.getAcceptOrds(acceptDocs));
+ return;
Review Comment:
nit: from a style perspective, I'd rather not return here and have an `else` block instead
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Utilize exact kNN search when gathering k > numVectors in a segment [lucene]
Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent merged PR #12806:
URL: https://github.com/apache/lucene/pull/12806
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org
Re: [PR] Utilize exact kNN search when gathering k > numVectors in a segment [lucene]
Posted by "benwtrent (via GitHub)" <gi...@apache.org>.
benwtrent commented on code in PR #12806:
URL: https://github.com/apache/lucene/pull/12806#discussion_r1393164542
##########
lucene/core/src/test/org/apache/lucene/search/BaseKnnVectorQueryTestCase.java:
##########
@@ -779,6 +781,16 @@ Directory getIndexStore(
doc.add(getKnnVectorField(field, contents[i], vectorSimilarityFunction));
doc.add(new StringField("id", "id" + i, Field.Store.YES));
writer.addDocument(doc);
+ if (randomBoolean()) {
Review Comment:
Adding docs in between vector docs to ensure our sparse vector reader is adequately exercised. Simple improvement in coverage here.
##########
lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java:
##########
@@ -200,6 +206,12 @@ protected TopDocs exactSearch(LeafReaderContext context, DocIdSetIterator accept
return new TopDocs(totalHits, topScoreDocs);
}
+ /**
+ * @param ctx the leaf reader context
+ * @return the number of vectors in the given leaf.
+ */
+ protected abstract int numVectorsInLeaf(LeafReaderContext ctx) throws IOException;
Review Comment:
This is cheap, we store it per field. This way we have a good count even when not every document has a vector.
##########
lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java:
##########
@@ -110,6 +110,12 @@ private TopDocs getLeafResults(LeafReaderContext ctx, Weight filterWeight) throw
int maxDoc = ctx.reader().maxDoc();
if (filterWeight == null) {
+ int cost = liveDocs == null ? maxDoc : liveDocs.length();
Review Comment:
I am fairly ignorant around what `liveDocs.length()` means. Here I am assuming if there are `liveDocs`, this iterator might actually be smaller than `maxDoc`.
##########
lucene/core/src/java/org/apache/lucene/search/AbstractKnnVectorQuery.java:
##########
@@ -171,13 +177,13 @@ protected TopDocs exactSearch(LeafReaderContext context, DocIdSetIterator accept
}
VectorScorer vectorScorer = createVectorScorer(context, fi);
+ // Iterate over the vector docs that match the filter and find the top k
+ DocIdSetIterator iterator =
+ ConjunctionUtils.intersectIterators(List.of(acceptIterator, vectorScorer.iterator()));
Review Comment:
An intersection of the vector values and the user provided filter seemed like the way it should have always been.
Do we want to remove the `fieldExists` query logic when a user provides a pre-filter?
--
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-unsubscribe@lucene.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org