Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusts Seeded knn searches to clean up user and internal interfaces #14170

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ API Changes
New Features
---------------------

* GITHUB#14084, GITHUB#13635, GITHUB#13634: Adds new `SeededKnnByteVectorQuery` and `SeededKnnFloatVectorQuery`
queries. These queries allow for the vector search entry points to be initialized via a `seed` query. This follows
* GITHUB#14084, GITHUB#13635, GITHUB#13634, GITHUB#14170: Adds new `SeededKnnVectorQuery` query.
These queries allow for the vector search entry points to be initialized via a `seed` query. This follows
the research provided via https://arxiv.org/abs/2307.16779. (Sean MacAvaney, Ben Trent).


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.lucene.search;

import org.apache.lucene.search.knn.KnnSearchStrategy;

/**
* AbstractKnnCollector is the default implementation for a knn collector used for gathering kNN
* results and providing topDocs from the gathered neighbors
Expand All @@ -25,11 +27,17 @@ public abstract class AbstractKnnCollector implements KnnCollector {

protected long visitedCount;
private final long visitLimit;
private final KnnSearchStrategy searchStrategy;
private final int k;

protected AbstractKnnCollector(int k, long visitLimit) {
this.visitLimit = visitLimit;
protected AbstractKnnCollector(int k, long visitLimit, KnnSearchStrategy searchStrategy) {
this.k = k;
this.searchStrategy = searchStrategy;
this.visitLimit = visitLimit;
}

protected AbstractKnnCollector(int k, long visitLimit) {
this(k, visitLimit, null);
}

@Override
Expand Down Expand Up @@ -68,4 +76,9 @@ public final int k() {

@Override
public abstract TopDocs topDocs();

@Override
public KnnSearchStrategy getSearchStrategy() {
return searchStrategy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ abstract class AbstractVectorSimilarityQuery extends Query {
}

protected KnnCollectorManager getKnnCollectorManager() {
return (visitedLimit, context) ->
new VectorSimilarityCollector(traversalSimilarity, resultSimilarity, visitedLimit);
return (visitLimit, searchStrategy, context) ->
new VectorSimilarityCollector(traversalSimilarity, resultSimilarity, visitLimit);
}

abstract VectorScorer createVectorScorer(LeafReaderContext context) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected TopDocs approximateSearch(
int visitLimit,
KnnCollectorManager knnCollectorManager)
throws IOException {
KnnCollector collector = knnCollectorManager.newCollector(visitLimit, context);
KnnCollector collector = knnCollectorManager.newCollector(visitLimit, null, context);
context.reader().searchNearestVectors(field, target, collector, acceptDocs);
return collector.topDocs();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected TopDocs approximateSearch(
int visitLimit,
KnnCollectorManager knnCollectorManager)
throws IOException {
KnnCollector collector = knnCollectorManager.newCollector(visitLimit, context);
KnnCollector collector = knnCollectorManager.newCollector(visitLimit, null, context);
context.reader().searchNearestVectors(field, target, collector, acceptDocs);
return collector.topDocs();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected TopDocs approximateSearch(
int visitedLimit,
KnnCollectorManager knnCollectorManager)
throws IOException {
KnnCollector knnCollector = knnCollectorManager.newCollector(visitedLimit, context);
KnnCollector knnCollector = knnCollectorManager.newCollector(visitedLimit, null, context);
LeafReader reader = context.reader();
ByteVectorValues byteVectorValues = reader.getByteVectorValues(field);
if (byteVectorValues == null) {
Expand Down
16 changes: 15 additions & 1 deletion lucene/core/src/java/org/apache/lucene/search/KnnCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.lucene.search;

import org.apache.lucene.search.knn.KnnSearchStrategy;

/**
* KnnCollector is a knn collector used for gathering kNN results and providing topDocs from the
* gathered neighbors
Expand Down Expand Up @@ -86,14 +88,21 @@ public interface KnnCollector {
*/
TopDocs topDocs();

/**
* @return the search strategy used by this collector, can be null
*/
default KnnSearchStrategy getSearchStrategy() {
return null;
}

/**
* KnnCollector.Decorator is the base class for decorators of KnnCollector objects, which extend
* the object with new behaviors.
*
* @lucene.experimental
*/
abstract class Decorator implements KnnCollector {
private final KnnCollector collector;
protected final KnnCollector collector;

public Decorator(KnnCollector collector) {
this.collector = collector;
Expand Down Expand Up @@ -138,5 +147,10 @@ public float minCompetitiveSimilarity() {
public TopDocs topDocs() {
return collector.topDocs();
}

@Override
public KnnSearchStrategy getSearchStrategy() {
return collector.getSearchStrategy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected TopDocs approximateSearch(
int visitedLimit,
KnnCollectorManager knnCollectorManager)
throws IOException {
KnnCollector knnCollector = knnCollectorManager.newCollector(visitedLimit, context);
KnnCollector knnCollector = knnCollectorManager.newCollector(visitedLimit, null, context);
LeafReader reader = context.reader();
FloatVectorValues floatVectorValues = reader.getFloatVectorValues(field);
if (floatVectorValues == null) {
Expand Down

This file was deleted.

This file was deleted.

Loading