Skip to content

Commit 068056c

Browse files
committed
refactor(semantic_text)raise mapper_parsing_exception for incompatible sparse_v index versions
1 parent 89176d1 commit 068056c

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/mapper/SemanticTextFieldMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import java.util.function.Function;
102102
import java.util.function.Supplier;
103103

104+
import static org.elasticsearch.index.IndexVersions.NEW_SPARSE_VECTOR;
104105
import static org.elasticsearch.index.IndexVersions.SEMANTIC_TEXT_DEFAULTS_TO_BBQ;
105106
import static org.elasticsearch.index.IndexVersions.SEMANTIC_TEXT_DEFAULTS_TO_BBQ_BACKPORT_8_X;
106107
import static org.elasticsearch.inference.TaskType.SPARSE_EMBEDDING;
@@ -155,6 +156,8 @@ public class SemanticTextFieldMapper extends FieldMapper implements InferenceFie
155156
public static final float DEFAULT_RESCORE_OVERSAMPLE = 3.0f;
156157

157158
static final String INDEX_OPTIONS_FIELD = "index_options";
159+
public static final String ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR = "Creating a [semantic_text] field with [sparse_vector] models"
160+
+ " is not supported on this index. Try using a [dense_vector] model or create a new index with version 8.11+.";
158161

159162
public static final TypeParser parser(Supplier<ModelRegistry> modelRegistry) {
160163
return new TypeParser(
@@ -1246,6 +1249,9 @@ private static Mapper.Builder createEmbeddingsField(
12461249
) {
12471250
return switch (modelSettings.taskType()) {
12481251
case SPARSE_EMBEDDING -> {
1252+
if (indexVersionCreated.before(NEW_SPARSE_VECTOR)) {
1253+
throw new IllegalArgumentException(ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR);
1254+
}
12491255
SparseVectorFieldMapper.Builder sparseVectorMapperBuilder = new SparseVectorFieldMapper.Builder(
12501256
CHUNKED_EMBEDDINGS_FIELD,
12511257
indexVersionCreated,

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.getEmbeddingsFieldName;
111111
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_ELSER_2_INFERENCE_ID;
112112
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_RESCORE_OVERSAMPLE;
113+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR;
113114
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.INDEX_OPTIONS_FIELD;
114115
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettings;
115116
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettingsOtherThan;
@@ -1748,6 +1749,28 @@ public static void addSemanticTextInferenceResults(
17481749
}
17491750
}
17501751

1752+
public void testOldIndexSemanticTextSparseVersionRaisesError() throws IOException {
1753+
final XContentBuilder fieldMapping = fieldMapping(b -> {
1754+
b.field("type", "semantic_text");
1755+
b.field("inference_id", "another_inference_id");
1756+
b.startObject("model_settings");
1757+
b.field("task_type", "sparse_embedding");
1758+
b.endObject();
1759+
});
1760+
1761+
MapperParsingException exception = assertThrows(
1762+
MapperParsingException.class,
1763+
() -> createMapperService(
1764+
fieldMapping,
1765+
true,
1766+
IndexVersions.V_8_0_0,
1767+
IndexVersionUtils.getPreviousVersion(IndexVersions.NEW_SPARSE_VECTOR)
1768+
)
1769+
);
1770+
assertTrue(exception.getMessage().contains(ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR));
1771+
assertTrue(exception.getRootCause() instanceof IllegalArgumentException);
1772+
}
1773+
17511774
static String randomFieldName(int numLevel) {
17521775
StringBuilder builder = new StringBuilder();
17531776
for (int i = 0; i < numLevel; i++) {

0 commit comments

Comments
 (0)