-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add two new "Seeded" Knn queries for seeded vector search #14084
Open
benwtrent
wants to merge
45
commits into
apache:main
Choose a base branch
from
benwtrent:seeds
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
60e3fab
implement seeded knn queries
82e7053
cleanup
7955148
ensure seed docs have a vector
40d972d
apply filter to seed queries
f36a4cd
Update lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsReader…
seanmacavaney 539b29a
Update lucene/core/src/java/org/apache/lucene/codecs/KnnVectorsReader…
seanmacavaney 3df6ad2
Update lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQue…
seanmacavaney 0508d87
Update lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSear…
seanmacavaney 732f69c
Update lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSear…
seanmacavaney c02b4cc
Update lucene/core/src/java/org/apache/lucene/search/AbstractKnnVecto…
seanmacavaney 285ebfe
Update lucene/core/src/java/org/apache/lucene/search/AbstractKnnVecto…
seanmacavaney b64d458
Merge branch 'main' into seeds
seanmacavaney 9f1be67
mapping docIds to ordinals
244f46b
fixed test warning
b73e7a3
fix test warning
3134132
tidy
69db4d4
Update lucene/core/src/java/org/apache/lucene/search/AbstractKnnVecto…
seanmacavaney fe4bef3
Update lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQue…
seanmacavaney 8e044f8
Update lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQu…
seanmacavaney 33231b3
Update lucene/core/src/java/org/apache/lucene/index/FloatVectorValues…
seanmacavaney 2e86e4f
Update lucene/core/src/java/org/apache/lucene/index/ByteVectorValues.…
seanmacavaney c0c18b2
Update lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSear…
seanmacavaney 6190aca
Update lucene/core/src/java/org/apache/lucene/util/hnsw/HnswGraphSear…
seanmacavaney a49ba2f
address review comments
8698b59
Merge branch 'main' into seeds
seanmacavaney 1f8a9f4
merge issues
58f34df
addresses review comments
fc2129f
refactor wip
e8417d3
consistent naming
440b0d0
javadoc
87e75ab
tidy
7b3350f
javadoc typo
5bb40c2
javadoc
216bfc4
test fixes
bccf15d
Merge branch 'main' into seeds
seanmacavaney b6725c7
merge resolution
0cfd99b
merging
dd63bb0
refactor as decorator
1635870
javadoc
c8a512a
apply decorator elsewhere
3cfe678
Merge branch 'main' into seeds
benwtrent cb6ab54
Refactor
benwtrent 6d0cb4f
removing unnecessary changes
benwtrent 4635ab8
Merge remote-tracking branch 'upstream/main' into seeds
benwtrent 04289ac
adding changes & address PR comments & fixing tests
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
lucene/core/src/java/org/apache/lucene/search/SeededKnnByteVectorQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import org.apache.lucene.search.knn.KnnCollectorManager; | ||
import org.apache.lucene.search.knn.SeededKnnCollectorManager; | ||
|
||
/** | ||
* This is a version of knn byte vector query that provides a query seed to initiate the vector | ||
* search. NOTE: The underlying format is free to ignore the provided seed | ||
* | ||
* <p>See <a href="https://dl.acm.org/doi/10.1145/3539618.3591715">"Lexically-Accelerated Dense | ||
* Retrieval"</a> (Kulkarni, Hrishikesh and MacAvaney, Sean and Goharian, Nazli and Frieder, Ophir). | ||
* In SIGIR '23: Proceedings of the 46th International ACM SIGIR Conference on Research and | ||
* Development in Information Retrieval Pages 152 - 162 | ||
* | ||
* @lucene.experimental | ||
*/ | ||
public class SeededKnnByteVectorQuery extends KnnByteVectorQuery { | ||
final Query seed; | ||
final Weight seedWeight; | ||
|
||
/** | ||
* Construct a new SeededKnnByteVectorQuery instance | ||
* | ||
* @param field knn byte vector field to query | ||
* @param target the query vector | ||
* @param k number of neighbors to return | ||
* @param filter a filter on the neighbors to return | ||
* @param seed a query seed to initiate the vector format search | ||
*/ | ||
public SeededKnnByteVectorQuery(String field, byte[] target, int k, Query filter, Query seed) { | ||
super(field, target, k, filter); | ||
this.seed = Objects.requireNonNull(seed); | ||
this.seedWeight = null; | ||
} | ||
|
||
SeededKnnByteVectorQuery(String field, byte[] target, int k, Query filter, Weight seedWeight) { | ||
super(field, target, k, filter); | ||
this.seed = null; | ||
this.seedWeight = Objects.requireNonNull(seedWeight); | ||
} | ||
|
||
@Override | ||
public Query rewrite(IndexSearcher indexSearcher) throws IOException { | ||
if (seedWeight != null) { | ||
return super.rewrite(indexSearcher); | ||
} | ||
BooleanQuery.Builder booleanSeedQueryBuilder = | ||
new BooleanQuery.Builder() | ||
.add(seed, BooleanClause.Occur.MUST) | ||
.add(new FieldExistsQuery(field), BooleanClause.Occur.FILTER); | ||
if (filter != null) { | ||
booleanSeedQueryBuilder.add(filter, BooleanClause.Occur.FILTER); | ||
} | ||
Query seedRewritten = indexSearcher.rewrite(booleanSeedQueryBuilder.build()); | ||
Weight seedWeight = indexSearcher.createWeight(seedRewritten, ScoreMode.TOP_SCORES, 1f); | ||
SeededKnnByteVectorQuery rewritten = | ||
new SeededKnnByteVectorQuery(field, target, k, filter, seedWeight); | ||
return rewritten.rewrite(indexSearcher); | ||
} | ||
|
||
@Override | ||
protected KnnCollectorManager getKnnCollectorManager(int k, IndexSearcher searcher) { | ||
if (seedWeight == null) { | ||
throw new UnsupportedOperationException("must be rewritten before constructing manager"); | ||
} | ||
return new SeededKnnCollectorManager( | ||
super.getKnnCollectorManager(k, searcher), | ||
seedWeight, | ||
k, | ||
leaf -> leaf.getFloatVectorValues(field)); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
lucene/core/src/java/org/apache/lucene/search/SeededKnnFloatVectorQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import org.apache.lucene.search.knn.KnnCollectorManager; | ||
import org.apache.lucene.search.knn.SeededKnnCollectorManager; | ||
|
||
/** | ||
* This is a version of knn float vector query that provides a query seed to initiate the vector | ||
* search. NOTE: The underlying format is free to ignore the provided seed. | ||
* | ||
* <p>See <a href="https://dl.acm.org/doi/10.1145/3539618.3591715">"Lexically-Accelerated Dense | ||
* Retrieval"</a> (Kulkarni, Hrishikesh and MacAvaney, Sean and Goharian, Nazli and Frieder, Ophir). | ||
* In SIGIR '23: Proceedings of the 46th International ACM SIGIR Conference on Research and | ||
* Development in Information Retrieval Pages 152 - 162 | ||
* | ||
* @lucene.experimental | ||
*/ | ||
public class SeededKnnFloatVectorQuery extends KnnFloatVectorQuery { | ||
final Query seed; | ||
final Weight seedWeight; | ||
|
||
/** | ||
* Construct a new SeededKnnFloatVectorQuery instance | ||
* | ||
* @param field knn float vector field to query | ||
* @param target the query vector | ||
* @param k number of neighbors to return | ||
* @param filter a filter on the neighbors to return | ||
* @param seed a query seed to initiate the vector format search | ||
*/ | ||
public SeededKnnFloatVectorQuery(String field, float[] target, int k, Query filter, Query seed) { | ||
super(field, target, k, filter); | ||
this.seed = Objects.requireNonNull(seed); | ||
this.seedWeight = null; | ||
} | ||
|
||
SeededKnnFloatVectorQuery(String field, float[] target, int k, Query filter, Weight seedWeight) { | ||
super(field, target, k, filter); | ||
this.seed = null; | ||
this.seedWeight = Objects.requireNonNull(seedWeight); | ||
} | ||
|
||
@Override | ||
public Query rewrite(IndexSearcher indexSearcher) throws IOException { | ||
if (seedWeight != null) { | ||
return super.rewrite(indexSearcher); | ||
} | ||
BooleanQuery.Builder booleanSeedQueryBuilder = | ||
new BooleanQuery.Builder() | ||
.add(seed, BooleanClause.Occur.MUST) | ||
.add(new FieldExistsQuery(field), BooleanClause.Occur.FILTER); | ||
if (filter != null) { | ||
booleanSeedQueryBuilder.add(filter, BooleanClause.Occur.FILTER); | ||
} | ||
Query seedRewritten = indexSearcher.rewrite(booleanSeedQueryBuilder.build()); | ||
Weight seedWeight = indexSearcher.createWeight(seedRewritten, ScoreMode.TOP_SCORES, 1f); | ||
SeededKnnFloatVectorQuery rewritten = | ||
new SeededKnnFloatVectorQuery(field, target, k, filter, seedWeight); | ||
return rewritten.rewrite(indexSearcher); | ||
} | ||
|
||
@Override | ||
protected KnnCollectorManager getKnnCollectorManager(int k, IndexSearcher searcher) { | ||
if (seedWeight == null) { | ||
throw new UnsupportedOperationException("must be rewritten before constructing manager"); | ||
} | ||
return new SeededKnnCollectorManager( | ||
super.getKnnCollectorManager(k, searcher), | ||
seedWeight, | ||
k, | ||
leaf -> leaf.getFloatVectorValues(field)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
lucene/core/src/java/org/apache/lucene/search/knn/EntryPointProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.knn; | ||
|
||
import org.apache.lucene.search.DocIdSetIterator; | ||
|
||
/** Provides entry points for the kNN search */ | ||
public interface EntryPointProvider { | ||
/** Iterator of valid entry points for the kNN search */ | ||
DocIdSetIterator entryPoints(); | ||
|
||
/** Number of valid entry points for the kNN search */ | ||
int numberOfEntryPoints(); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe