Skip to content

Commit 4e4fae2

Browse files
committed
fix(semantic_text): index underlying dense_vector field in older indices
1 parent c7fcf8b commit 4e4fae2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

server/src/main/java/org/elasticsearch/index/mapper/vectors/DenseVectorFieldMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ protected Parameter<?>[] getParameters() {
406406
return new Parameter<?>[] { elementType, dims, indexed, similarity, indexOptions, meta };
407407
}
408408

409+
public Builder indexed(boolean indexed) {
410+
this.indexed.setValue(indexed);
411+
return this;
412+
}
413+
409414
public Builder similarity(VectorSimilarity vectorSimilarity) {
410415
similarity.setValue(vectorSimilarity);
411416
return this;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.cluster.metadata.InferenceFieldMetadata;
2626
import org.elasticsearch.common.Strings;
2727
import org.elasticsearch.common.bytes.BytesReference;
28+
import org.elasticsearch.common.logging.DeprecationCategory;
2829
import org.elasticsearch.common.xcontent.XContentHelper;
2930
import org.elasticsearch.common.xcontent.XContentParserUtils;
3031
import org.elasticsearch.common.xcontent.support.XContentMapValues;
@@ -101,8 +102,10 @@
101102
import java.util.function.Function;
102103
import java.util.function.Supplier;
103104

105+
import static org.elasticsearch.index.IndexVersions.NEW_SPARSE_VECTOR;
104106
import static org.elasticsearch.index.IndexVersions.SEMANTIC_TEXT_DEFAULTS_TO_BBQ;
105107
import static org.elasticsearch.index.IndexVersions.SEMANTIC_TEXT_DEFAULTS_TO_BBQ_BACKPORT_8_X;
108+
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper.INDEXED_BY_DEFAULT_INDEX_VERSION;
106109
import static org.elasticsearch.inference.TaskType.SPARSE_EMBEDDING;
107110
import static org.elasticsearch.inference.TaskType.TEXT_EMBEDDING;
108111
import static org.elasticsearch.lucene.search.uhighlight.CustomUnifiedHighlighter.MULTIVAL_SEP_CHAR;
@@ -127,6 +130,11 @@
127130
*/
128131
public class SemanticTextFieldMapper extends FieldMapper implements InferenceFieldMapper {
129132
private static final Logger logger = LogManager.getLogger(SemanticTextFieldMapper.class);
133+
// 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. ...";
130138
public static final NodeFeature SEMANTIC_TEXT_IN_OBJECT_FIELD_FIX = new NodeFeature("semantic_text.in_object_field_fix");
131139
public static final NodeFeature SEMANTIC_TEXT_SINGLE_FIELD_UPDATE_FIX = new NodeFeature("semantic_text.single_field_update_fix");
132140
public static final NodeFeature SEMANTIC_TEXT_DELETE_FIX = new NodeFeature("semantic_text.delete_fix");
@@ -1276,6 +1284,10 @@ private static void configureSparseVectorMapperBuilder(
12761284
SparseVectorFieldMapper.Builder sparseVectorMapperBuilder,
12771285
SemanticTextIndexOptions indexOptions
12781286
) {
1287+
if (indexVersionCreated.before(NEW_SPARSE_VECTOR)) {
1288+
throw new IllegalArgumentException(ERROR_MESSAGE_UNSUPPORTED_SPARSE_VECTOR);
1289+
}
1290+
12791291
if (indexOptions != null) {
12801292
SparseVectorFieldMapper.SparseVectorIndexOptions sparseVectorIndexOptions =
12811293
(SparseVectorFieldMapper.SparseVectorIndexOptions) indexOptions.indexOptions();
@@ -1296,6 +1308,13 @@ private static void configureDenseVectorMapperBuilder(
12961308
MinimalServiceSettings modelSettings,
12971309
SemanticTextIndexOptions indexOptions
12981310
) {
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+
}
1317+
12991318
SimilarityMeasure similarity = modelSettings.similarity();
13001319
if (similarity != null) {
13011320
switch (similarity) {

0 commit comments

Comments
 (0)