Skip to content

Commit 5bd6ec3

Browse files
committed
Add validation for element_type when using vector quantization
1 parent 57f00e4 commit 5bd6ec3

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/main/java/org/springframework/data/elasticsearch/core/index/MappingParameters.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,31 +372,34 @@ public void writeTypeAndParametersTo(ObjectNode objectNode) throws IOException {
372372
}
373373

374374
if (knnSimilarity != KnnSimilarity.DEFAULT) {
375-
Assert.isTrue(index, "knn similarity can only be specified when \"index\" is true.");
375+
Assert.isTrue(index, "knn similarity can only be specified when 'index' is true.");
376376
objectNode.put(FIELD_PARAM_SIMILARITY, knnSimilarity.getSimilarity());
377377
}
378378

379379
if (knnIndexOptions != null) {
380-
Assert.isTrue(index, "knn similarity can only be specified when \"index\" is true.");
380+
Assert.isTrue(index, "knn index options can only be specified when 'index' is true.");
381381
ObjectNode indexOptionsNode = objectNode.putObject(FIELD_PARAM_INDEX_OPTIONS);
382-
if (knnIndexOptions.type() != KnnAlgorithmType.DEFAULT) {
383-
indexOptionsNode.put(FIELD_PARAM_TYPE, knnIndexOptions.type().getType());
382+
KnnAlgorithmType algoType = knnIndexOptions.type();
383+
if (algoType != KnnAlgorithmType.DEFAULT) {
384+
if (algoType == KnnAlgorithmType.INT8_HNSW || algoType == KnnAlgorithmType.INT8_FLAT) {
385+
Assert.isTrue(!FieldElementType.BYTE.equals(elementType),
386+
"'element_type' can only be float when using vector quantization.");
387+
}
388+
indexOptionsNode.put(FIELD_PARAM_TYPE, algoType.getType());
384389
}
385390
if (knnIndexOptions.m() >= 0) {
386-
Assert.isTrue(knnIndexOptions.type() == KnnAlgorithmType.HNSW
387-
|| knnIndexOptions.type() == KnnAlgorithmType.INT8_HNSW,
391+
Assert.isTrue(algoType == KnnAlgorithmType.HNSW || algoType == KnnAlgorithmType.INT8_HNSW,
388392
"knn 'm' parameter can only be applicable to hnsw and int8_hnsw index types.");
389393
indexOptionsNode.put(FIELD_PARAM_M, knnIndexOptions.m());
390394
}
391395
if (knnIndexOptions.efConstruction() >= 0) {
392-
Assert.isTrue(knnIndexOptions.type() == KnnAlgorithmType.HNSW
393-
|| knnIndexOptions.type() == KnnAlgorithmType.INT8_HNSW,
396+
Assert.isTrue(algoType == KnnAlgorithmType.HNSW || algoType == KnnAlgorithmType.INT8_HNSW,
394397
"knn 'ef_construction' can only be applicable to hnsw and int8_hnsw index types.");
395398
indexOptionsNode.put(FIELD_PARAM_EF_CONSTRUCTION, knnIndexOptions.efConstruction());
396399
}
397400
if (knnIndexOptions.confidenceInterval() >= 0) {
398-
Assert.isTrue(knnIndexOptions.type() == KnnAlgorithmType.INT8_HNSW
399-
|| knnIndexOptions.type() == KnnAlgorithmType.INT8_FLAT,
401+
Assert.isTrue(algoType == KnnAlgorithmType.INT8_HNSW
402+
|| algoType == KnnAlgorithmType.INT8_FLAT,
400403
"knn 'confidence_interval' can only be applicable to int8_hnsw and int8_flat index types.");
401404
indexOptionsNode.put(FIELD_PARAM_CONFIDENCE_INTERVAL, knnIndexOptions.confidenceInterval());
402405
}

0 commit comments

Comments
 (0)