-
Notifications
You must be signed in to change notification settings - Fork 25.5k
Create a version of bbq_hnsw that supports on_disk_rescore #135931
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
207475c
Create a format for bbq_hnsw with direct IO support
thecoop 361bc62
Merge branch 'main' into bbq_hnsw-disk-rescoring
thecoop 681e3cf
Pull out generic fields helper
thecoop e77c8ff
Make generic format store per-field
thecoop 3c434c2
Write out during merging too
thecoop a0c3005
Merge branch 'main' into bbq_hnsw-disk-rescoring
thecoop 8f7312c
Use direct IO in tests sometimes
thecoop 8bfeb84
PR comments
thecoop 878fa71
Merge branch 'main' into bbq_hnsw-disk-rescoring
thecoop c9a1d02
Add a no-op scorer implementation
thecoop a4da2e5
Merge branch 'main' into bbq_hnsw-disk-rescoring
thecoop 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 hidden or 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 hidden or 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
92 changes: 92 additions & 0 deletions
92
server/src/main/java/org/elasticsearch/index/codec/vectors/GenericFlatVectorReaders.java
This file contains hidden or 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,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.codec.vectors; | ||
|
||
import org.apache.lucene.codecs.hnsw.FlatVectorsReader; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Keeps track of field-specific raw vector readers for vector reads | ||
*/ | ||
public class GenericFlatVectorReaders { | ||
|
||
public interface Field { | ||
String rawVectorFormatName(); | ||
|
||
boolean useDirectIOReads(); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface LoadFlatVectorsReader { | ||
FlatVectorsReader getReader(String formatName, boolean useDirectIO) throws IOException; | ||
} | ||
|
||
private record FlatVectorsReaderKey(String formatName, boolean useDirectIO) { | ||
private FlatVectorsReaderKey(Field field) { | ||
this(field.rawVectorFormatName(), field.useDirectIOReads()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return formatName + (useDirectIO ? " with Direct IO" : ""); | ||
} | ||
} | ||
|
||
private final Map<FlatVectorsReaderKey, FlatVectorsReader> readers = new HashMap<>(); | ||
private final Map<Integer, FlatVectorsReader> readersForFields = new HashMap<>(); | ||
|
||
public void loadField(int fieldNumber, Field field, LoadFlatVectorsReader loadReader) throws IOException { | ||
FlatVectorsReaderKey key = new FlatVectorsReaderKey(field); | ||
FlatVectorsReader reader = readers.get(key); | ||
if (reader == null) { | ||
reader = loadReader.getReader(field.rawVectorFormatName(), field.useDirectIOReads()); | ||
if (reader == null) { | ||
throw new IllegalStateException("Cannot find flat vector format: " + field.rawVectorFormatName()); | ||
} | ||
readers.put(key, reader); | ||
} | ||
readersForFields.put(fieldNumber, reader); | ||
} | ||
|
||
public FlatVectorsReader getReaderForField(int fieldNumber) { | ||
FlatVectorsReader reader = readersForFields.get(fieldNumber); | ||
if (reader == null) { | ||
throw new IllegalArgumentException("Invalid field number [" + fieldNumber + "]"); | ||
} | ||
return reader; | ||
} | ||
|
||
public Collection<FlatVectorsReader> allReaders() { | ||
return Collections.unmodifiableCollection(readers.values()); | ||
} | ||
|
||
public GenericFlatVectorReaders getMergeInstance() throws IOException { | ||
GenericFlatVectorReaders mergeReaders = new GenericFlatVectorReaders(); | ||
|
||
// link the original instance with the merge instance | ||
Map<FlatVectorsReader, FlatVectorsReader> mergeInstances = new IdentityHashMap<>(); | ||
for (var reader : readers.entrySet()) { | ||
FlatVectorsReader mergeInstance = reader.getValue().getMergeInstance(); | ||
mergeInstances.put(reader.getValue(), mergeInstance); | ||
mergeReaders.readers.put(reader.getKey(), mergeInstance); | ||
} | ||
// link up the fields to the merge readers | ||
for (var field : readersForFields.entrySet()) { | ||
mergeReaders.readersForFields.put(field.getKey(), mergeInstances.get(field.getValue())); | ||
} | ||
return mergeReaders; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.