Skip to content

Commit a6eb387

Browse files
committed
update msg, change versions, add ut
1 parent e63a078 commit a6eb387

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@
131131
public class SemanticTextFieldMapper extends FieldMapper implements InferenceFieldMapper {
132132
private static final Logger logger = LogManager.getLogger(SemanticTextFieldMapper.class);
133133
// TODO: rewrite the warning and error messages below, just placeholders for now
134-
static final String WARNING_MESSAGE_8X = "The [dense_vector] field type created by `semantic_text` uses default index settings which may not be optimal. " +
135-
"Consider creating a new index ....";
136-
private static final String ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR = "The [sparse_vector] field type created by `semantic_text` is not supported on indices created on versions 8.0 to 8.10." +
137-
"Try using a `dense_vector` model or upgrade your index to 8.11 or later. ...";
134+
public static final String WARNING_MESSAGE_8X = "Creating a `semantic_text` field on this index version may not include" +
135+
" optimized default settings. Consider creating a new index for a better performance.";
136+
public static final String ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR = "Creating a `semantic_text` field with `sparse_vector` models" +
137+
" is not supported on indices created with versions 8.0-8.10." +
138+
" Try using a `dense_vector` model or create a new index with version 8.11+.";
138139
public static final NodeFeature SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX = new NodeFeature("semantic_text.in_object_field_fix");
139140
public static final NodeFeature SEMANTIC_TEXT_SINGLE_FIELD_UPDATE_FIX = new NodeFeature("semantic_text.single_field_update_fix");
140141
public static final NodeFeature SEMANTIC_TEXT_DELETE_FIX = new NodeFeature("semantic_text.delete_fix");
@@ -1308,12 +1309,10 @@ private static void configureDenseVectorMapperBuilder(
13081309
MinimalServiceSettings modelSettings,
13091310
SemanticTextIndexOptions indexOptions
13101311
) {
1311-
if (indexVersionCreated.onOrAfter(IndexVersions.V_8_0_0)){
1312-
if (indexVersionCreated.before(INDEXED_BY_DEFAULT_INDEX_VERSION)) {
1313-
deprecationLogger.warn(DeprecationCategory.MAPPINGS, "semantic_text", WARNING_MESSAGE_8X);
1314-
}
1315-
denseVectorMapperBuilder.indexed(true);
1316-
}
1312+
if (indexVersionCreated.before(INDEXED_BY_DEFAULT_INDEX_VERSION)) {
1313+
deprecationLogger.warn(DeprecationCategory.MAPPINGS, "semantic_text", WARNING_MESSAGE_8X);
1314+
denseVectorMapperBuilder.indexed(true);
1315+
}
13171316

13181317
SimilarityMeasure similarity = modelSettings.similarity();
13191318
if (similarity != null) {

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
1111

12+
import org.apache.logging.log4j.Level;
1213
import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
1314
import org.apache.lucene.document.FeatureField;
1415
import org.apache.lucene.index.FieldInfo;
@@ -27,15 +28,23 @@
2728
import org.apache.lucene.search.join.ScoreMode;
2829
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
2930
import org.elasticsearch.cluster.ClusterChangedEvent;
31+
import org.elasticsearch.cluster.metadata.DataStreamGlobalRetention;
32+
import org.elasticsearch.cluster.metadata.DataStreamLifecycle;
3033
import org.elasticsearch.cluster.metadata.IndexMetadata;
3134
import org.elasticsearch.common.CheckedBiConsumer;
3235
import org.elasticsearch.common.CheckedBiFunction;
3336
import org.elasticsearch.common.Strings;
3437
import org.elasticsearch.common.bytes.BytesReference;
3538
import org.elasticsearch.common.compress.CompressedXContent;
39+
import org.elasticsearch.common.logging.DeprecatedMessage;
40+
import org.elasticsearch.common.logging.DeprecationLogger;
41+
import org.elasticsearch.common.logging.ESLogMessage;
42+
import org.elasticsearch.common.logging.HeaderWarning;
3643
import org.elasticsearch.common.lucene.search.Queries;
3744
import org.elasticsearch.common.settings.Settings;
45+
import org.elasticsearch.common.util.concurrent.ThreadContext;
3846
import org.elasticsearch.core.CheckedConsumer;
47+
import org.elasticsearch.core.TimeValue;
3948
import org.elasticsearch.index.IndexVersion;
4049
import org.elasticsearch.index.IndexVersions;
4150
import org.elasticsearch.index.mapper.DocumentMapper;
@@ -110,7 +119,9 @@
110119
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.getEmbeddingsFieldName;
111120
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_ELSER_2_INFERENCE_ID;
112121
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_RESCORE_OVERSAMPLE;
122+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR;
113123
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.INDEX_OPTIONS_FIELD;
124+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.WARNING_MESSAGE_8X;
114125
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettings;
115126
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettingsOtherThan;
116127
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText;
@@ -414,6 +425,53 @@ public void testInvalidTaskTypes() {
414425
}
415426
}
416427

428+
@Override
429+
protected String[] getParseMinimalWarnings() {
430+
return new String[] {
431+
WARNING_MESSAGE_8X
432+
};
433+
}
434+
435+
public void testOldIndexSemanticTextDenseVectorCreation() throws IOException {
436+
final String fieldName = "field";
437+
final XContentBuilder fieldMapping = fieldMapping(b -> {
438+
b.field("type", "semantic_text");
439+
b.field(INFERENCE_ID_FIELD, "test_inference_id");
440+
b.startObject("model_settings");
441+
b.field("task_type", "text_embedding");
442+
b.field("dimensions", 384);
443+
b.field("similarity", "cosine");
444+
b.field("element_type", "float");
445+
b.endObject();
446+
});
447+
448+
MapperService mapperService = createMapperService(
449+
fieldMapping,
450+
true,
451+
IndexVersions.V_8_0_0,
452+
IndexVersionUtils.getPreviousVersion(IndexVersions.FIRST_DETACHED_INDEX_VERSION));
453+
assertParseMinimalWarnings();
454+
assertSemanticTextField(mapperService, fieldName, true, null, null);
455+
}
456+
457+
public void testOldIndexSemanticTextSparseVersionRaisesError() throws IOException {
458+
final XContentBuilder fieldMapping = fieldMapping(b -> {
459+
b.field("type", "semantic_text");
460+
b.field("inference_id", "another_inference_id");
461+
b.startObject("model_settings");
462+
b.field("task_type", "sparse_embedding");
463+
b.endObject();
464+
});
465+
466+
MapperParsingException exception = assertThrows(MapperParsingException.class, () -> createMapperService(
467+
fieldMapping,
468+
true,
469+
IndexVersions.V_8_0_0,
470+
IndexVersionUtils.getPreviousVersion(IndexVersions.NEW_SPARSE_VECTOR)));
471+
assertTrue(exception.getMessage().contains(ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR));
472+
assertTrue(exception.getRootCause() instanceof IllegalArgumentException);
473+
}
474+
417475
public void testMultiFieldsSupport() throws IOException {
418476
if (useLegacyFormat) {
419477
Exception e = expectThrows(MapperParsingException.class, () -> createMapperService(fieldMapping(b -> {

0 commit comments

Comments
 (0)