| 
111 | 111 | import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_ELSER_2_INFERENCE_ID;  | 
112 | 112 | import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_RESCORE_OVERSAMPLE;  | 
113 | 113 | import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.INDEX_OPTIONS_FIELD;  | 
 | 114 | +import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.UNSUPPORTED_INDEX_MESSAGE;  | 
114 | 115 | import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettings;  | 
115 | 116 | import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettingsOtherThan;  | 
116 | 117 | import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText;  | 
@@ -414,6 +415,57 @@ public void testInvalidTaskTypes() {  | 
414 | 415 |         }  | 
415 | 416 |     }  | 
416 | 417 | 
 
  | 
 | 418 | +    @Override  | 
 | 419 | +    protected IndexVersion boostNotAllowedIndexVersion() {  | 
 | 420 | +        return IndexVersions.NEW_SPARSE_VECTOR;  | 
 | 421 | +    }  | 
 | 422 | + | 
 | 423 | +    public void testOldIndexSemanticTextDenseVectorRaisesError() throws IOException {  | 
 | 424 | +        final String fieldName = "field";  | 
 | 425 | +        final XContentBuilder fieldMapping = fieldMapping(b -> {  | 
 | 426 | +            b.field("type", "semantic_text");  | 
 | 427 | +            b.field(INFERENCE_ID_FIELD, "test_inference_id");  | 
 | 428 | +            b.startObject("model_settings");  | 
 | 429 | +            b.field("task_type", "text_embedding");  | 
 | 430 | +            b.field("dimensions", 384);  | 
 | 431 | +            b.field("similarity", "cosine");  | 
 | 432 | +            b.field("element_type", "float");  | 
 | 433 | +            b.endObject();  | 
 | 434 | +        });  | 
 | 435 | +        assertOldIndexUnsupported(fieldMapping);  | 
 | 436 | +    }  | 
 | 437 | + | 
 | 438 | +    public void testOldIndexSemanticTextMinimalMappingRaisesError() throws IOException {  | 
 | 439 | +        final XContentBuilder fieldMapping = fieldMapping(this::minimalMapping);  | 
 | 440 | +        assertOldIndexUnsupported(fieldMapping);  | 
 | 441 | +    }  | 
 | 442 | + | 
 | 443 | +    public void testOldIndexSemanticTextSparseVersionRaisesError() throws IOException {  | 
 | 444 | +        final XContentBuilder fieldMapping = fieldMapping(b -> {  | 
 | 445 | +            b.field("type", "semantic_text");  | 
 | 446 | +            b.field("inference_id", "another_inference_id");  | 
 | 447 | +            b.startObject("model_settings");  | 
 | 448 | +            b.field("task_type", "sparse_embedding");  | 
 | 449 | +            b.endObject();  | 
 | 450 | +        });  | 
 | 451 | +        assertOldIndexUnsupported(fieldMapping);  | 
 | 452 | +    }  | 
 | 453 | + | 
 | 454 | +    private void assertOldIndexUnsupported(XContentBuilder fieldMapping) {  | 
 | 455 | + | 
 | 456 | +        MapperParsingException exception = assertThrows(  | 
 | 457 | +            MapperParsingException.class,  | 
 | 458 | +            () -> createMapperService(  | 
 | 459 | +                fieldMapping,  | 
 | 460 | +                true,  | 
 | 461 | +                IndexVersions.V_8_0_0,  | 
 | 462 | +                IndexVersionUtils.getPreviousVersion(IndexVersions.NEW_SPARSE_VECTOR)  | 
 | 463 | +            )  | 
 | 464 | +        );  | 
 | 465 | +        assertTrue(exception.getMessage().contains(UNSUPPORTED_INDEX_MESSAGE));  | 
 | 466 | +        assertTrue(exception.getRootCause() instanceof UnsupportedOperationException);  | 
 | 467 | +    }  | 
 | 468 | + | 
417 | 469 |     public void testMultiFieldsSupport() throws IOException {  | 
418 | 470 |         if (useLegacyFormat) {  | 
419 | 471 |             Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {  | 
 | 
0 commit comments