Skip to content

Commit 55652ca

Browse files
Add version check for full field name validation (opensearch-project#2477) (opensearch-project#2502)
Signed-off-by: Kunal Kotwani <[email protected]> (cherry picked from commit 811ae2e) Co-authored-by: Kunal Kotwani <[email protected]>
1 parent aa3551b commit 55652ca

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
4545
* 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)
4646
* 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]
4747
* Fixing bug where mapping accepts both dimension and model-id (#2410)[https://github.com/opensearch-project/k-NN/pull/2410]
48+
* Add version check for full field name validation (#2477)[https://github.com/opensearch-project/k-NN/pull/2477]
4849
### Infrastructure
4950
* Updated C++ version in JNI from c++11 to c++17 [#2259](https://github.com/opensearch-project/k-NN/pull/2259)
5051
* 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)

Diff for: src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapper.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.createKNNMethodContextFromLegacy;
5959
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.createStoredFieldForByteVector;
6060
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.createStoredFieldForFloatVector;
61+
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.useFullFieldNameValidation;
6162
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.validateIfCircuitBreakerIsNotTriggered;
6263
import static org.opensearch.knn.index.mapper.KNNVectorFieldMapperUtil.validateIfKNNPluginEnabled;
6364
import static org.opensearch.knn.index.mapper.ModelFieldMapper.UNSET_MODEL_DIMENSION_IDENTIFIER;
@@ -240,7 +241,9 @@ protected Explicit<Boolean> ignoreMalformed(BuilderContext context) {
240241

241242
@Override
242243
public KNNVectorFieldMapper build(BuilderContext context) {
243-
validateFullFieldName(context);
244+
if (useFullFieldNameValidation(indexCreatedVersion)) {
245+
validateFullFieldName(context);
246+
}
244247

245248
final MultiFields multiFieldsBuilder = this.multiFieldsBuilder.build(this, context);
246249
final CopyTo copyToBuilder = copyTo.build();

Diff for: src/main/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapperUtil.java

+10
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ static boolean useLuceneKNNVectorsFormat(final Version indexCreatedVersion) {
146146
return indexCreatedVersion.onOrAfter(Version.V_2_17_0);
147147
}
148148

149+
/**
150+
* Determines if full field name validation should be applied based on the index creation version.
151+
*
152+
* @param indexCreatedVersion The version when the index was created
153+
* @return true if the index version is 2.17.0 or later, false otherwise
154+
*/
155+
static boolean useFullFieldNameValidation(final Version indexCreatedVersion) {
156+
return indexCreatedVersion != null && indexCreatedVersion.onOrAfter(Version.V_2_17_0);
157+
}
158+
149159
public static SpaceType getSpaceType(final Settings indexSettings) {
150160
String spaceType = indexSettings.get(KNNSettings.INDEX_KNN_SPACE_TYPE.getKey());
151161
if (spaceType == null) {

Diff for: src/test/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapperTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ public void testMethodFieldMapperParseCreateField_validInput_thenDifferentFieldT
11771177
when(parseContext.parser()).thenReturn(createXContentParser(dataType));
11781178

11791179
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useLuceneKNNVectorsFormat(Mockito.any())).thenReturn(true);
1180+
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useFullFieldNameValidation(Mockito.any())).thenReturn(true);
11801181

11811182
OriginalMappingParameters originalMappingParameters = new OriginalMappingParameters(
11821183
dataType,
@@ -1222,6 +1223,7 @@ public void testMethodFieldMapperParseCreateField_validInput_thenDifferentFieldT
12221223
);
12231224

12241225
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useLuceneKNNVectorsFormat(Mockito.any())).thenReturn(false);
1226+
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useFullFieldNameValidation(Mockito.any())).thenReturn(false);
12251227

12261228
document = new ParseContext.Document();
12271229
contentPath = new ContentPath();
@@ -1287,6 +1289,7 @@ public void testModelFieldMapperParseCreateField_validInput_thenDifferentFieldTy
12871289
when(parseContext.parser()).thenReturn(createXContentParser(dataType));
12881290

12891291
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useLuceneKNNVectorsFormat(Mockito.any())).thenReturn(true);
1292+
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useFullFieldNameValidation(Mockito.any())).thenReturn(true);
12901293

12911294
OriginalMappingParameters originalMappingParameters = new OriginalMappingParameters(
12921295
VectorDataType.DEFAULT,
@@ -1335,6 +1338,7 @@ public void testModelFieldMapperParseCreateField_validInput_thenDifferentFieldTy
13351338
);
13361339

13371340
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useLuceneKNNVectorsFormat(Mockito.any())).thenReturn(false);
1341+
utilMockedStatic.when(() -> KNNVectorFieldMapperUtil.useFullFieldNameValidation(Mockito.any())).thenReturn(true);
13381342

13391343
document = new ParseContext.Document();
13401344
contentPath = new ContentPath();

Diff for: src/test/java/org/opensearch/knn/index/mapper/KNNVectorFieldMapperUtilTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ public void testUseLuceneKNNVectorsFormat_withDifferentInputs_thenSuccess() {
7777
Assert.assertTrue(KNNVectorFieldMapperUtil.useLuceneKNNVectorsFormat(Version.V_2_17_0));
7878
Assert.assertTrue(KNNVectorFieldMapperUtil.useLuceneKNNVectorsFormat(Version.V_3_0_0));
7979
}
80+
81+
/**
82+
* Test useFullFieldNameValidation method for different OpenSearch versions
83+
*/
84+
public void testUseFullFieldNameValidation() {
85+
Assert.assertFalse(KNNVectorFieldMapperUtil.useFullFieldNameValidation(Version.V_2_16_0));
86+
Assert.assertTrue(KNNVectorFieldMapperUtil.useFullFieldNameValidation(Version.V_2_17_0));
87+
Assert.assertTrue(KNNVectorFieldMapperUtil.useFullFieldNameValidation(Version.V_2_18_0));
88+
}
8089
}

0 commit comments

Comments
 (0)