Skip to content

Commit d142366

Browse files
authored
Fixing bug where mapping accepts both dimension and model-id (#2410)
Signed-off-by: Mark Wu <wumr@amazon.com>
1 parent 90fdad4 commit d142366

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4242
* Fix filter rewrite logic which was resulting in getting inconsistent / incorrect results for cases where filter was getting rewritten for shards (#2359)[https://github.com/opensearch-project/k-NN/pull/2359]
4343
* Fixing it to retrieve space_type from index setting when both method and top level don't have the value. [#2374](https://github.com/opensearch-project/k-NN/pull/2374)
4444
* Fixing the bug where setting rescore as false for on_disk knn_vector query is a no-op (#2399)[https://github.com/opensearch-project/k-NN/pull/2399]
45+
* Fixing bug where mapping accepts both dimension and model-id (#2410)[https://github.com/opensearch-project/k-NN/pull/2410]
4546
### Infrastructure
4647
* Updated C++ version in JNI from c++11 to c++17 [#2259](https://github.com/opensearch-project/k-NN/pull/2259)
4748
* Upgrade bytebuddy and objenesis version to match OpenSearch core and, update github ci runner for macos [#2279](https://github.com/opensearch-project/k-NN/pull/2279)

src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapper.java

+9
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ public Mapper.Builder<?> parse(String name, Map<String, Object> node, ParserCont
378378
);
379379
}
380380

381+
// Ensure user-defined dimension and model are mutually exclusive for indicies created after 2.19
382+
if (builder.dimension.getValue() != UNSET_MODEL_DIMENSION_IDENTIFIER
383+
&& builder.modelId.get() != null
384+
&& parserContext.indexVersionCreated().onOrAfter(Version.V_2_19_0)) {
385+
throw new IllegalArgumentException(
386+
String.format(Locale.ROOT, "Cannot specify both a modelId and dimension in the mapping: %s", name)
387+
);
388+
}
389+
381390
// Check for flat configuration and validate only if index is created after 2.17
382391
if (isKNNDisabled(parserContext.getSettings()) && parserContext.indexVersionCreated().onOrAfter(Version.V_2_17_0)) {
383392
validateFromFlat(builder);

src/test/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapperTests.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,6 @@ public void testTypeParser_parse_compressionAndModeParameter() {
681681
XContentBuilder xContentBuilder4 = XContentFactory.jsonBuilder()
682682
.startObject()
683683
.field(TYPE_FIELD_NAME, KNN_VECTOR_TYPE)
684-
.field(DIMENSION, 10)
685684
.field(VECTOR_DATA_TYPE_FIELD, VectorDataType.DEFAULT.getValue())
686685
.field(MODEL_ID, "test")
687686
.field(MODE_PARAMETER, Mode.ON_DISK.getName())
@@ -892,6 +891,27 @@ public void testTypeParser_parse_fromModel() throws IOException {
892891
);
893892

894893
assertEquals(modelId, builder.modelId.get());
894+
895+
// Fails if both model_id and dimension are set after 2.19
896+
XContentBuilder xContentBuilder2 = XContentFactory.jsonBuilder()
897+
.startObject()
898+
.field(TYPE_FIELD_NAME, KNN_VECTOR_TYPE)
899+
.field(DIMENSION_FIELD_NAME, TEST_DIMENSION)
900+
.field(MODEL_ID, modelId)
901+
.endObject();
902+
903+
expectThrows(
904+
IllegalArgumentException.class,
905+
() -> typeParser.parse(fieldName, xContentBuilderToMap(xContentBuilder2), buildParserContext(indexName, settings))
906+
);
907+
908+
// Should not fail if both are set before 2.19
909+
typeParser.parse(
910+
fieldName,
911+
xContentBuilderToMap(xContentBuilder2),
912+
buildLegacyParserContext(indexName, settings, Version.V_2_18_1)
913+
);
914+
895915
}
896916

897917
public void testTypeParser_parse_fromLegacy() throws IOException {

0 commit comments

Comments
 (0)