-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Adding 'dense_vector' field type #3659
Closed
martin-gaievski
wants to merge
7
commits into
opensearch-project:main
from
martin-gaievski:lucene-knn
+1,552
−10
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15fc51d
Adding 'dense_vector' field type
martin-gaievski d317584
Adding checks on size limits for field type and vector data
martin-gaievski 65c0944
Updating license header, exclude ES grant part
martin-gaievski 0325d0b
Review comments: inherit hasValues from base class, error messages, j…
martin-gaievski ecb18fd
Refactor mapper to extend FieldMapper, adjust tests accordingly
martin-gaievski 6ad1bda
Remove redundant null-check
martin-gaievski 24dfcdb
Refactoring code to address comments
martin-gaievski 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
98 changes: 98 additions & 0 deletions
98
server/src/internalClusterTest/java/org/opensearch/search/knn/DenseVectorIT.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,98 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.search.knn; | ||
|
||
import org.opensearch.Version; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.test.OpenSearchIntegTestCase; | ||
import org.opensearch.test.VersionUtils; | ||
|
||
import java.util.Map; | ||
|
||
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; | ||
import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; | ||
|
||
public class DenseVectorIT extends OpenSearchIntegTestCase { | ||
|
||
private static final float[] VECTOR_ONE = { 2.0f, 4.5f, 5.6f, 4.2f }; | ||
private static final float[] VECTOR_TWO = { 4.0f, 2.5f, 1.6f, 2.2f }; | ||
|
||
@Override | ||
protected boolean forbidPrivateIndexSettings() { | ||
return false; | ||
} | ||
|
||
public void testIndexingSingleDocumentWithoutKnn() throws Exception { | ||
Version version = VersionUtils.randomIndexCompatibleVersion(random()); | ||
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build(); | ||
XContentBuilder defaultMapping = jsonBuilder().startObject() | ||
.startObject("properties") | ||
.startObject("vector_field") | ||
.field("type", "dense_vector") | ||
.field("dimension", 4) | ||
.endObject() | ||
.endObject() | ||
.endObject(); | ||
assertAcked(prepareCreate("test").setSettings(settings).setMapping(defaultMapping)); | ||
ensureGreen(); | ||
|
||
indexRandom( | ||
true, | ||
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("vector_field", VECTOR_ONE).endObject()) | ||
); | ||
ensureSearchable("test"); | ||
} | ||
|
||
public void testIndexingSingleDocumentWithDefaultKnnParams() throws Exception { | ||
Version version = VersionUtils.randomIndexCompatibleVersion(random()); | ||
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build(); | ||
XContentBuilder defaultMapping = jsonBuilder().startObject() | ||
.startObject("properties") | ||
.startObject("vector_field") | ||
.field("type", "dense_vector") | ||
.field("dimension", 4) | ||
.field("knn", Map.of()) | ||
.endObject() | ||
.endObject() | ||
.endObject(); | ||
assertAcked(prepareCreate("test").setSettings(settings).setMapping(defaultMapping)); | ||
ensureGreen(); | ||
|
||
indexRandom( | ||
true, | ||
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("vector_field", VECTOR_ONE).endObject()) | ||
); | ||
ensureSearchable("test"); | ||
} | ||
|
||
public void testIndexingMultipleDocumentsWithHnswDefinition() throws Exception { | ||
Version version = VersionUtils.randomIndexCompatibleVersion(random()); | ||
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build(); | ||
XContentBuilder defaultMapping = jsonBuilder().startObject() | ||
.startObject("properties") | ||
.startObject("field") | ||
.field("type", "dense_vector") | ||
.field("dimension", 4) | ||
.field( | ||
"knn", | ||
Map.of("metric", "l2", "algorithm", Map.of("name", "hnsw", "parameters", Map.of("max_connections", 12, "beam_width", 256))) | ||
) | ||
.endObject() | ||
.endObject() | ||
.endObject(); | ||
assertAcked(prepareCreate("test").setSettings(settings).setMapping(defaultMapping)); | ||
ensureGreen(); | ||
|
||
indexRandom( | ||
true, | ||
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("vector_field", VECTOR_ONE).endObject()), | ||
client().prepareIndex("test").setId("2").setSource(jsonBuilder().startObject().field("vector_field", VECTOR_TWO).endObject()) | ||
); | ||
ensureSearchable("test"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
server/src/main/java/org/opensearch/index/codec/KnnVectorFormatFactory.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,62 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.index.codec; | ||
|
||
import org.apache.lucene.codecs.KnnVectorsFormat; | ||
import org.apache.lucene.codecs.lucene92.Lucene92Codec; | ||
import org.apache.lucene.codecs.lucene92.Lucene92HnswVectorsFormat; | ||
import org.opensearch.index.mapper.KnnAlgorithmContext; | ||
import org.opensearch.index.mapper.MappedFieldType; | ||
import org.opensearch.index.mapper.MapperService; | ||
|
||
import java.util.Map; | ||
|
||
import static org.opensearch.index.mapper.DenseVectorFieldMapper.DenseVectorFieldType; | ||
import static org.opensearch.index.mapper.KnnAlgorithmContextFactory.HNSW_PARAMETER_BEAM_WIDTH; | ||
import static org.opensearch.index.mapper.KnnAlgorithmContextFactory.HNSW_PARAMETER_MAX_CONNECTIONS; | ||
|
||
/** | ||
* Factory that creates a {@link KnnVectorsFormat knn vector format} based on a mapping | ||
* configuration for the field. | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class KnnVectorFormatFactory { | ||
|
||
private final MapperService mapperService; | ||
|
||
public KnnVectorFormatFactory(MapperService mapperService) { | ||
this.mapperService = mapperService; | ||
} | ||
|
||
/** | ||
* Create KnnVectorsFormat with parameters specified in the field definition or return codec's default | ||
* Knn Vector Format if field is not of DenseVector type | ||
* @param field name of the field | ||
* @return KnnVectorFormat that is specific to a mapped field | ||
*/ | ||
public KnnVectorsFormat create(final String field) { | ||
final MappedFieldType mappedFieldType = mapperService.fieldType(field); | ||
if (isDenseVectorFieldType(mappedFieldType)) { | ||
final DenseVectorFieldType knnVectorFieldType = (DenseVectorFieldType) mappedFieldType; | ||
final KnnAlgorithmContext algorithmContext = knnVectorFieldType.getKnnContext().getKnnAlgorithmContext(); | ||
final Map<String, Object> methodParams = algorithmContext.getParameters(); | ||
int maxConnections = getIntegerParam(methodParams, HNSW_PARAMETER_MAX_CONNECTIONS); | ||
int beamWidth = getIntegerParam(methodParams, HNSW_PARAMETER_BEAM_WIDTH); | ||
final KnnVectorsFormat luceneHnswVectorsFormat = new Lucene92HnswVectorsFormat(maxConnections, beamWidth); | ||
return luceneHnswVectorsFormat; | ||
} | ||
return Lucene92Codec.getDefault().knnVectorsFormat(); | ||
} | ||
|
||
private boolean isDenseVectorFieldType(final MappedFieldType mappedFieldType) { | ||
return mappedFieldType instanceof DenseVectorFieldType; | ||
} | ||
|
||
private int getIntegerParam(Map<String, Object> methodParams, String name) { | ||
return (Integer) methodParams.get(name); | ||
} | ||
} |
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
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.
WDYT?
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'm not sure to be honest - we're wrapping Lucene classes and they have notation as "Knn" (e.g. https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/document/KnnVectorField.java), so I've follow them