Skip to content

Commit 78bf4a2

Browse files
committed
Add tests
1 parent 40ed2fd commit 78bf4a2

File tree

3 files changed

+287
-6
lines changed

3 files changed

+287
-6
lines changed

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapperTests.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,117 @@ public void testDefaultIndexOptions() throws IOException {
13281328
assertSemanticTextField(mapperService, "field", true, null, defaultDenseVectorSemanticIndexOptions());
13291329
}
13301330

1331+
public void testSpecifiedDenseVectorIndexOptions() throws IOException {
1332+
1333+
// Specifying index options will override default index option settings
1334+
var mapperService = createMapperService(fieldMapping(b -> {
1335+
b.field("type", "semantic_text");
1336+
b.field("inference_id", "another_inference_id");
1337+
b.startObject("model_settings");
1338+
b.field("task_type", "text_embedding");
1339+
b.field("dimensions", 100);
1340+
b.field("similarity", "cosine");
1341+
b.field("element_type", "float");
1342+
b.endObject();
1343+
b.startObject("index_options");
1344+
b.startObject("dense_vector");
1345+
b.field("type", "int4_hnsw");
1346+
b.field("m", 20);
1347+
b.field("ef_construction", 90);
1348+
b.field("confidence_interval", 0.4);
1349+
b.endObject();
1350+
b.endObject();
1351+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT);
1352+
assertSemanticTextField(
1353+
mapperService,
1354+
"field",
1355+
true,
1356+
null,
1357+
new SemanticTextIndexOptions(
1358+
SemanticTextIndexOptions.SupportedIndexOptions.DENSE_VECTOR,
1359+
new DenseVectorFieldMapper.Int4HnswIndexOptions(20, 90, 0.4f, null)
1360+
)
1361+
);
1362+
1363+
// Specifying partial index options will in the remainder index options with defaults
1364+
mapperService = createMapperService(fieldMapping(b -> {
1365+
b.field("type", "semantic_text");
1366+
b.field("inference_id", "another_inference_id");
1367+
b.startObject("model_settings");
1368+
b.field("task_type", "text_embedding");
1369+
b.field("dimensions", 100);
1370+
b.field("similarity", "cosine");
1371+
b.field("element_type", "float");
1372+
b.endObject();
1373+
b.startObject("index_options");
1374+
b.startObject("dense_vector");
1375+
b.field("type", "int4_hnsw");
1376+
b.endObject();
1377+
b.endObject();
1378+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT);
1379+
assertSemanticTextField(
1380+
mapperService,
1381+
"field",
1382+
true,
1383+
null,
1384+
new SemanticTextIndexOptions(
1385+
SemanticTextIndexOptions.SupportedIndexOptions.DENSE_VECTOR,
1386+
new DenseVectorFieldMapper.Int4HnswIndexOptions(16, 100, 0f, null)
1387+
)
1388+
);
1389+
1390+
// Incompatible index options will fail
1391+
Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {
1392+
b.field("type", "semantic_text");
1393+
b.field("inference_id", "another_inference_id");
1394+
b.startObject("model_settings");
1395+
b.field("task_type", "sparse_embedding");
1396+
b.endObject();
1397+
b.startObject("index_options");
1398+
b.startObject("dense_vector");
1399+
b.field("type", "int8_hnsw");
1400+
b.endObject();
1401+
b.endObject();
1402+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT));
1403+
assertThat(e.getMessage(), containsString("Invalid task type"));
1404+
1405+
e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {
1406+
b.field("type", "semantic_text");
1407+
b.field("inference_id", "another_inference_id");
1408+
b.startObject("model_settings");
1409+
b.field("task_type", "text_embedding");
1410+
b.field("dimensions", 100);
1411+
b.field("similarity", "cosine");
1412+
b.field("element_type", "float");
1413+
b.endObject();
1414+
b.startObject("index_options");
1415+
b.startObject("dense_vector");
1416+
b.field("type", "bbq_flat");
1417+
b.field("ef_construction", 100);
1418+
b.endObject();
1419+
b.endObject();
1420+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT));
1421+
assertThat(e.getMessage(), containsString("unsupported parameters: [ef_construction : 100]"));
1422+
1423+
e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {
1424+
b.field("type", "semantic_text");
1425+
b.field("inference_id", "another_inference_id");
1426+
b.startObject("model_settings");
1427+
b.field("task_type", "text_embedding");
1428+
b.field("dimensions", 100);
1429+
b.field("similarity", "cosine");
1430+
b.field("element_type", "float");
1431+
b.endObject();
1432+
b.startObject("index_options");
1433+
b.startObject("dense_vector");
1434+
b.field("type", "invalid");
1435+
b.endObject();
1436+
b.endObject();
1437+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT));
1438+
assertThat(e.getMessage(), containsString("Unsupported index options type invalid"));
1439+
1440+
}
1441+
13311442
public static SemanticTextIndexOptions randomSemanticTextIndexOptions() {
13321443
TaskType taskType = randomFrom(TaskType.SPARSE_EMBEDDING, TaskType.TEXT_EMBEDDING);
13331444
return randomSemanticTextIndexOptions(taskType);

x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/10_semantic_text_field_mapping.yml

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,15 +722,15 @@ setup:
722722
foo: bar
723723

724724
---
725-
"Model is required for index options":
725+
"Specifying index options requires model information":
726726
- requires:
727727
cluster_features: "semantic_text.index_options"
728728
reason: Index options introduced in 8.19.0
729729

730730
- do:
731731
catch: /Model settings must be set to validate index options/
732732
indices.create:
733-
index: test-no-model
733+
index: my-custom-semantic-index
734734
body:
735735
settings:
736736
index:
@@ -746,10 +746,11 @@ setup:
746746
dense_vector:
747747
type: int8_hnsw
748748

749+
- match: { status: 400 }
749750

750751
- do:
751752
indices.create:
752-
index: test-no-model
753+
index: my-custom-semantic-index
753754
body:
754755
settings:
755756
index:
@@ -760,3 +761,87 @@ setup:
760761
properties:
761762
semantic_field:
762763
type: semantic_text
764+
inference_id: nonexistent-inference-id
765+
766+
- do:
767+
indices.get_mapping:
768+
index: my-custom-semantic-index
769+
770+
- match: { "my-custom-semantic-index.mappings.properties.semantic_field.type": semantic_text }
771+
- match: { "my-custom-semantic-index.mappings.properties.semantic_field.inference_id": nonexistent-inference-id }
772+
- not_exists: my-custom-semantic-index.mappings.properties.semantic_field.index_options
773+
774+
---
775+
"Updating index options":
776+
- requires:
777+
cluster_features: "semantic_text.index_options"
778+
reason: Index options introduced in 8.19.0
779+
780+
- do:
781+
indices.create:
782+
index: test-index-options
783+
body:
784+
settings:
785+
index:
786+
mapping:
787+
semantic_text:
788+
use_legacy_format: false
789+
mappings:
790+
properties:
791+
semantic_field:
792+
type: semantic_text
793+
inference_id: dense-inference-id
794+
index_options:
795+
dense_vector:
796+
type: int8_hnsw
797+
m: 16
798+
ef_construction: 100
799+
confidence_interval: 1.0
800+
801+
- do:
802+
indices.get_mapping:
803+
index: test-index-options
804+
805+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.type": "int8_hnsw" }
806+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.m": 16 }
807+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.ef_construction": 100 }
808+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.confidence_interval": 1.0 }
809+
810+
- do:
811+
indices.put_mapping:
812+
index: test-index-options
813+
body:
814+
properties:
815+
semantic_field:
816+
type: semantic_text
817+
inference_id: dense-inference-id
818+
index_options:
819+
dense_vector:
820+
type: int8_hnsw
821+
m: 20
822+
ef_construction: 90
823+
confidence_interval: 1.0
824+
825+
- do:
826+
indices.get_mapping:
827+
index: test-index-options
828+
829+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.type": "int8_hnsw" }
830+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.m": 20 }
831+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.ef_construction": 90 }
832+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.confidence_interval": 1.0 }
833+
834+
- do:
835+
catch: /Incompatible index options/
836+
indices.put_mapping:
837+
index: test-index-options
838+
body:
839+
properties:
840+
semantic_field:
841+
type: semantic_text
842+
inference_id: dense-inference-id
843+
index_options:
844+
dense_vector:
845+
type: int8_flat
846+
847+
- match: { status: 400 }

x-pack/plugin/inference/src/yamlRestTest/resources/rest-api-spec/test/inference/10_semantic_text_field_mapping_bwc.yml

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,15 @@ setup:
677677
foo: bar
678678

679679
---
680-
"Model is required for index options":
680+
"Specifying index options requires model information":
681681
- requires:
682682
cluster_features: "semantic_text.index_options"
683683
reason: Index options introduced in 8.19.0
684684

685685
- do:
686686
catch: /Model settings must be set to validate index options/
687687
indices.create:
688-
index: test-no-model
688+
index: my-custom-semantic-index
689689
body:
690690
settings:
691691
index:
@@ -701,10 +701,11 @@ setup:
701701
dense_vector:
702702
type: int8_hnsw
703703

704+
- match: { status: 400 }
704705

705706
- do:
706707
indices.create:
707-
index: test-no-model
708+
index: my-custom-semantic-index
708709
body:
709710
settings:
710711
index:
@@ -715,3 +716,87 @@ setup:
715716
properties:
716717
semantic_field:
717718
type: semantic_text
719+
inference_id: nonexistent-inference-id
720+
721+
- do:
722+
indices.get_mapping:
723+
index: my-custom-semantic-index
724+
725+
- match: { "my-custom-semantic-index.mappings.properties.semantic_field.type": semantic_text }
726+
- match: { "my-custom-semantic-index.mappings.properties.semantic_field.inference_id": nonexistent-inference-id }
727+
- not_exists: my-custom-semantic-index.mappings.properties.semantic_field.index_options
728+
729+
---
730+
"Updating index options":
731+
- requires:
732+
cluster_features: "semantic_text.index_options"
733+
reason: Index options introduced in 8.19.0
734+
735+
- do:
736+
indices.create:
737+
index: test-index-options
738+
body:
739+
settings:
740+
index:
741+
mapping:
742+
semantic_text:
743+
use_legacy_format: true
744+
mappings:
745+
properties:
746+
semantic_field:
747+
type: semantic_text
748+
inference_id: dense-inference-id
749+
index_options:
750+
dense_vector:
751+
type: int8_hnsw
752+
m: 16
753+
ef_construction: 100
754+
confidence_interval: 1.0
755+
756+
- do:
757+
indices.get_mapping:
758+
index: test-index-options
759+
760+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.type": "int8_hnsw" }
761+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.m": 16 }
762+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.ef_construction": 100 }
763+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.confidence_interval": 1.0 }
764+
765+
- do:
766+
indices.put_mapping:
767+
index: test-index-options
768+
body:
769+
properties:
770+
semantic_field:
771+
type: semantic_text
772+
inference_id: dense-inference-id
773+
index_options:
774+
dense_vector:
775+
type: int8_hnsw
776+
m: 20
777+
ef_construction: 90
778+
confidence_interval: 1.0
779+
780+
- do:
781+
indices.get_mapping:
782+
index: test-index-options
783+
784+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.type": "int8_hnsw" }
785+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.m": 20 }
786+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.ef_construction": 90 }
787+
- match: { "test-index-options.mappings.properties.semantic_field.index_options.dense_vector.confidence_interval": 1.0 }
788+
789+
- do:
790+
catch: /Incompatible index options/
791+
indices.put_mapping:
792+
index: test-index-options
793+
body:
794+
properties:
795+
semantic_field:
796+
type: semantic_text
797+
inference_id: dense-inference-id
798+
index_options:
799+
dense_vector:
800+
type: int8_flat
801+
802+
- match: { status: 400 }

0 commit comments

Comments
 (0)