Skip to content

Commit b50831a

Browse files
committed
fill in test specific sparse vector index options
1 parent b434f8b commit b50831a

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

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

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,77 @@ public void testSpecifiedDenseVectorIndexOptions() throws IOException {
15351535
}
15361536

15371537
public void testSpecificSparseVectorIndexOptions() throws IOException {
1538-
// TODO
1538+
for (int i=0; i < 10; i++) {
1539+
SparseVectorFieldMapper.SparseVectorIndexOptions testIndexOptions = randomSparseVectorIndexOptionsAll();
1540+
var mapperService = createMapperService(fieldMapping(b -> {
1541+
b.field("type", SemanticTextFieldMapper.CONTENT_TYPE);
1542+
b.field(INFERENCE_ID_FIELD, "test_inference_id");
1543+
b.startObject(INDEX_OPTIONS_FIELD);
1544+
{
1545+
b.startObject("sparse_vector");
1546+
{
1547+
setSparseVectorIndexOptionInMapper(b, testIndexOptions.getPrune(), testIndexOptions.getPruningConfig());
1548+
}
1549+
b.endObject();
1550+
}
1551+
b.endObject();
1552+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT);
1553+
1554+
assertSemanticTextField(
1555+
mapperService,
1556+
"field",
1557+
false,
1558+
null,
1559+
new SemanticTextIndexOptions(
1560+
SemanticTextIndexOptions.SupportedIndexOptions.SPARSE_VECTOR,
1561+
testIndexOptions
1562+
)
1563+
);
1564+
}
1565+
1566+
Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {
1567+
b.field("type", SemanticTextFieldMapper.CONTENT_TYPE);
1568+
b.field(INFERENCE_ID_FIELD, "test_inference_id");
1569+
b.startObject(INDEX_OPTIONS_FIELD);
1570+
{
1571+
b.startObject("sparse_vector");
1572+
{
1573+
b.field("prune", false);
1574+
b.startObject("pruning_config");
1575+
{
1576+
b.field(TokenPruningConfig.TOKENS_FREQ_RATIO_THRESHOLD.getPreferredName(), 5.0f);
1577+
}
1578+
b.endObject();
1579+
}
1580+
b.endObject();
1581+
}
1582+
b.endObject();
1583+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT));
1584+
assertThat(
1585+
e.getMessage(),
1586+
containsString("[index_options] field [pruning_config] should only be set if [prune] is set to true")
1587+
);
1588+
1589+
e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {
1590+
b.field("type", SemanticTextFieldMapper.CONTENT_TYPE);
1591+
b.field(INFERENCE_ID_FIELD, "test_inference_id");
1592+
b.startObject(INDEX_OPTIONS_FIELD);
1593+
{
1594+
b.startObject("sparse_vector");
1595+
{
1596+
b.field("prune", true);
1597+
b.startObject("pruning_config");
1598+
{
1599+
b.field(TokenPruningConfig.TOKENS_FREQ_RATIO_THRESHOLD.getPreferredName(), 1000.0f);
1600+
}
1601+
b.endObject();
1602+
}
1603+
b.endObject();
1604+
}
1605+
b.endObject();
1606+
}), useLegacyFormat, IndexVersions.INFERENCE_METADATA_FIELDS_BACKPORT));
1607+
var innerClause = e.getCause().getCause().getCause();
1608+
assertThat(innerClause.getMessage(), containsString("[tokens_freq_ratio_threshold] must be between [1] and [100], got 1000.0"));
15391609
}
15401610

15411611
public static SemanticTextIndexOptions randomSemanticTextIndexOptions() {

0 commit comments

Comments
 (0)