-
Notifications
You must be signed in to change notification settings - Fork 230
Add base64 binary encoding as default format for knn_vector docvalue_fields #3324
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
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
96 changes: 96 additions & 0 deletions
96
src/main/java/org/opensearch/knn/index/KNNVectorDocValueFormat.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,96 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.knn.index; | ||
|
|
||
| import lombok.Getter; | ||
| import org.opensearch.core.common.io.stream.StreamInput; | ||
| import org.opensearch.core.common.io.stream.StreamOutput; | ||
| import org.opensearch.search.DocValueFormat; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
| import java.util.Arrays; | ||
| import java.util.Base64; | ||
|
|
||
| /** | ||
| * DocValueFormat for knn_vector fields. Supports two modes: | ||
| * <ul> | ||
| * <li>{@code array} — vectors are returned as JSON numeric arrays</li> | ||
| * <li>{@code binary} (default) — vectors are returned as base64-encoded little-endian byte strings (with padding)</li> | ||
| * </ul> | ||
| */ | ||
| @Getter | ||
| public enum KNNVectorDocValueFormat implements DocValueFormat { | ||
|
|
||
| ARRAY_FORMAT("array", false), | ||
| BINARY_FORMAT("binary", true); | ||
|
|
||
| public static final String NAME = "knn_vector"; | ||
|
|
||
| private final String formatName; | ||
| private final boolean binary; | ||
| private static final Base64.Encoder BASE64_ENCODER = Base64.getEncoder(); | ||
|
|
||
| KNNVectorDocValueFormat(final String formatName, boolean binary) { | ||
| this.formatName = formatName; | ||
| this.binary = binary; | ||
| } | ||
|
|
||
| public static KNNVectorDocValueFormat fromStream(final StreamInput in) throws IOException { | ||
| return in.readBoolean() ? BINARY_FORMAT : ARRAY_FORMAT; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the format string to the corresponding enum constant. | ||
| * Returns {@link #BINARY_FORMAT} when format is null (the default). | ||
| * | ||
| * @param format the format string from the docvalue_fields request, or null for default | ||
| * @return the matching {@link KNNVectorDocValueFormat} | ||
| * @throws IllegalArgumentException if the format string is not recognized | ||
| */ | ||
| public static KNNVectorDocValueFormat fromFormatString(final String format) { | ||
| if (format == null || BINARY_FORMAT.formatName.equals(format)) { | ||
| return BINARY_FORMAT; | ||
| } | ||
| if (ARRAY_FORMAT.formatName.equals(format)) { | ||
| return ARRAY_FORMAT; | ||
| } | ||
| throw new IllegalArgumentException( | ||
| "Unsupported knn_vector docvalue_fields format [" | ||
| + format | ||
| + "]. Supported formats are " | ||
| + Arrays.toString(KNNVectorDocValueFormat.values()) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public String getWriteableName() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(final StreamOutput out) throws IOException { | ||
| out.writeBoolean(binary); | ||
| } | ||
|
|
||
| /** | ||
| * Encodes a float[] vector as a base64 string with little-endian byte order. | ||
| * Each float is written as 4 bytes in little-endian format (native byte order on x86/ARM), | ||
| * then the resulting byte array is base64-encoded. | ||
| */ | ||
| public static String encodeToBinary(final float[] vector) { | ||
| final ByteBuffer buffer = ByteBuffer.allocate(vector.length * Float.BYTES).order(ByteOrder.LITTLE_ENDIAN); | ||
| buffer.asFloatBuffer().put(vector); // Bulk operation optimized by the JVM | ||
| final byte[] bytes = buffer.array(); | ||
| return BASE64_ENCODER.encodeToString(bytes); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "knn_vector(" + formatName + ")"; | ||
| } | ||
| } |
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.
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.