- Notifications
You must be signed in to change notification settings - Fork 25.6k
Improve brute force vector search speed by using Lucene functions #96617
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
Changes from 2 commits
12dcc1b 80bb47a 5e9ba58 e060d4f 39a8e9b ce40541 4b42476 789dae8 67be030 File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 96617 | ||
| summary: Improve brute force vector search speed by using Lucene functions | ||
| area: Search | ||
| type: enhancement | ||
| issues: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -13,21 +13,24 @@ | |
| import org.elasticsearch.Version; | ||
| import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.ElementType; | ||
| import org.elasticsearch.index.mapper.vectors.DenseVectorScriptDocValues; | ||
| import org.elasticsearch.index.mapper.vectors.VectorEncoderDecoder; | ||
| | ||
| import java.io.IOException; | ||
| | ||
| public class BinaryDenseVectorDocValuesField extends DenseVectorDocValuesField { | ||
| | ||
| protected final BinaryDocValues input; | ||
| protected final Version indexVersion; | ||
| protected final int dims; | ||
| protected BytesRef value; | ||
| private final BinaryDocValues input; | ||
| private final float[] vectorValue; | ||
| private final Version indexVersion; | ||
| private final int dims; | ||
| private BytesRef value; | ||
| | ||
| public BinaryDenseVectorDocValuesField(BinaryDocValues input, String name, ElementType elementType, int dims, Version indexVersion) { | ||
| super(name, elementType); | ||
| this.input = input; | ||
| this.indexVersion = indexVersion; | ||
| this.dims = dims; | ||
| this.vectorValue = new float[dims]; | ||
| } | ||
| | ||
| @Override | ||
| | @@ -54,16 +57,17 @@ public DenseVector get() { | |
| if (isEmpty()) { | ||
| return DenseVector.EMPTY; | ||
| } | ||
| | ||
| return new BinaryDenseVector(value, dims, indexVersion); | ||
| VectorEncoderDecoder.decodeDenseVector(value, vectorValue); | ||
| return new BinaryDenseVector(vectorValue, value, dims, indexVersion); | ||
| } | ||
| | ||
| @Override | ||
| public DenseVector get(DenseVector defaultValue) { | ||
| if (isEmpty()) { | ||
| return defaultValue; | ||
| } | ||
| return new BinaryDenseVector(value, dims, indexVersion); | ||
| VectorEncoderDecoder.decodeDenseVector(value, vectorValue); | ||
| ||
| return new BinaryDenseVector(vectorValue, value, dims, indexVersion); | ||
| } | ||
| | ||
| @Override | ||
| | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
I would expect that
vectorBR.offsetis not needed here. In fact it would be a problem if it is anything other than0?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.
@ChrisHegarty I guess I misunderstand. I thought
getFloatwas the absolute position within the underlyingbytes, not relative to the starting position (dictated by the wrapping of bytes with offset).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.
My equation is wrong for sure, I think it should be
(dim * Float.BYTES) + vectorBR.offset?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.
This test passes with my fixed equation:
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.
@ChrisHegarty what do you think? I fixed my math. Is this still an issue?
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.
@benwtrent Your changes are good. The wrap with an offset and length is not relevant any more - it could be a plain
wrap(byte[])- since we are now using absolute addressing of the buffer - the completebyte[]is available to us even withwrap(byte[],int,int), which just sets up an initial position and limit.