Skip to content

Commit 66d65b5

Browse files
committed
Move index_options parsing to semantic text field mapper
1 parent 3036adf commit 66d65b5

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public record SemanticTextField(
7373
static final String CHUNKED_END_OFFSET_FIELD = "end_offset";
7474
static final String MODEL_SETTINGS_FIELD = "model_settings";
7575
static final String CHUNKING_SETTINGS_FIELD = "chunking_settings";
76-
static final String INDEX_OPTIONS_FIELD = "index_options";
7776

7877
public record InferenceResult(
7978
String inferenceId,
@@ -142,25 +141,6 @@ static ChunkingSettings parseChunkingSettingsFromMap(Object node) {
142141
}
143142
}
144143

145-
static SemanticTextIndexOptions parseIndexOptionsFromMap(String fieldName, Object node, IndexVersion indexVersion) {
146-
147-
if (node == null) {
148-
return null;
149-
}
150-
151-
Map<String, Object> map = XContentMapValues.nodeMapValue(node, INDEX_OPTIONS_FIELD);
152-
if (map.size() != 1) {
153-
throw new IllegalArgumentException("Too many index options provided, found [" + map.keySet() + "]");
154-
}
155-
Map.Entry<String, Object> entry = map.entrySet().iterator().next();
156-
SemanticTextIndexOptions.SupportedIndexOptions indexOptions = SemanticTextIndexOptions.SupportedIndexOptions.fromValue(
157-
entry.getKey()
158-
);
159-
@SuppressWarnings("unchecked")
160-
Map<String, Object> indexOptionsMap = (Map<String, Object>) entry.getValue();
161-
return new SemanticTextIndexOptions(indexOptions, indexOptions.parseIndexOptions(fieldName, indexOptionsMap, indexVersion));
162-
}
163-
164144
@Override
165145
public List<String> originalValues() {
166146
return originalValues != null ? originalValues : Collections.emptyList();
@@ -247,8 +227,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
247227
String inferenceId = (String) args[0];
248228
MinimalServiceSettings modelSettings = (MinimalServiceSettings) args[1];
249229
Map<String, Object> chunkingSettings = (Map<String, Object>) args[2];
250-
SemanticTextIndexOptions indexOptions = (SemanticTextIndexOptions) args[3];
251-
Map<String, List<Chunk>> chunks = (Map<String, List<Chunk>>) args[4];
230+
Map<String, List<Chunk>> chunks = (Map<String, List<Chunk>>) args[3];
252231
return new InferenceResult(inferenceId, modelSettings, ChunkingSettingsBuilder.fromMap(chunkingSettings, false), chunks);
253232
}
254233
);
@@ -282,12 +261,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
282261
null,
283262
new ParseField(CHUNKING_SETTINGS_FIELD)
284263
);
285-
INFERENCE_RESULT_PARSER.declareObjectOrNull(
286-
optionalConstructorArg(),
287-
(p, c) -> parseIndexOptionsFromMap(c.fieldName, p.map(), c.indexVersion()),
288-
null,
289-
new ParseField(INDEX_OPTIONS_FIELD)
290-
);
291264
INFERENCE_RESULT_PARSER.declareField(constructorArg(), (p, c) -> {
292265
if (c.useLegacyFormat()) {
293266
return Map.of(c.fieldName, parseChunksArrayLegacy(p, c));

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.bytes.BytesReference;
2828
import org.elasticsearch.common.xcontent.XContentHelper;
2929
import org.elasticsearch.common.xcontent.XContentParserUtils;
30+
import org.elasticsearch.common.xcontent.support.XContentMapValues;
3031
import org.elasticsearch.core.CheckedConsumer;
3132
import org.elasticsearch.core.Nullable;
3233
import org.elasticsearch.features.NodeFeature;
@@ -107,7 +108,6 @@
107108
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_OFFSET_FIELD;
108109
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKING_SETTINGS_FIELD;
109110
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKS_FIELD;
110-
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INDEX_OPTIONS_FIELD;
111111
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INFERENCE_FIELD;
112112
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INFERENCE_ID_FIELD;
113113
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.MODEL_SETTINGS_FIELD;
@@ -145,6 +145,8 @@ public class SemanticTextFieldMapper extends FieldMapper implements InferenceFie
145145

146146
public static final float DEFAULT_RESCORE_OVERSAMPLE = 3.0f;
147147

148+
static final String INDEX_OPTIONS_FIELD = "index_options";
149+
148150
public static final TypeParser parser(Supplier<ModelRegistry> modelRegistry) {
149151
return new TypeParser(
150152
(n, c) -> new Builder(n, c::bitSetProducer, c.getIndexSettings(), modelRegistry.get()),
@@ -205,7 +207,7 @@ public static class Builder extends FieldMapper.Builder {
205207
INDEX_OPTIONS_FIELD,
206208
true,
207209
() -> null,
208-
(n, c, o) -> SemanticTextField.parseIndexOptionsFromMap(n, o, c.indexVersionCreated()),
210+
(n, c, o) -> parseIndexOptionsFromMap(n, o, c.indexVersionCreated()),
209211
mapper -> ((SemanticTextFieldType) mapper.fieldType()).indexOptions,
210212
XContentBuilder::field,
211213
Objects::toString
@@ -1272,4 +1274,23 @@ private static boolean canMergeIndexOptions(SemanticTextIndexOptions previous, S
12721274

12731275
return true;
12741276
}
1277+
1278+
private static SemanticTextIndexOptions parseIndexOptionsFromMap(String fieldName, Object node, IndexVersion indexVersion) {
1279+
1280+
if (node == null) {
1281+
return null;
1282+
}
1283+
1284+
Map<String, Object> map = XContentMapValues.nodeMapValue(node, INDEX_OPTIONS_FIELD);
1285+
if (map.size() != 1) {
1286+
throw new IllegalArgumentException("Too many index options provided, found [" + map.keySet() + "]");
1287+
}
1288+
Map.Entry<String, Object> entry = map.entrySet().iterator().next();
1289+
SemanticTextIndexOptions.SupportedIndexOptions indexOptions = SemanticTextIndexOptions.SupportedIndexOptions.fromValue(
1290+
entry.getKey()
1291+
);
1292+
@SuppressWarnings("unchecked")
1293+
Map<String, Object> indexOptionsMap = (Map<String, Object>) entry.getValue();
1294+
return new SemanticTextIndexOptions(indexOptions, indexOptions.parseIndexOptions(fieldName, indexOptionsMap, indexVersion));
1295+
}
12751296
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
import static org.elasticsearch.index.mapper.vectors.DenseVectorFieldTypeTests.randomIndexOptionsAll;
9898
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKED_EMBEDDINGS_FIELD;
9999
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.CHUNKS_FIELD;
100-
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INDEX_OPTIONS_FIELD;
101100
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INFERENCE_FIELD;
102101
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.INFERENCE_ID_FIELD;
103102
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.MODEL_SETTINGS_FIELD;
@@ -107,6 +106,7 @@
107106
import static org.elasticsearch.xpack.inference.mapper.SemanticTextField.getEmbeddingsFieldName;
108107
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_ELSER_2_INFERENCE_ID;
109108
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.DEFAULT_RESCORE_OVERSAMPLE;
109+
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldMapper.INDEX_OPTIONS_FIELD;
110110
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettings;
111111
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.generateRandomChunkingSettingsOtherThan;
112112
import static org.elasticsearch.xpack.inference.mapper.SemanticTextFieldTests.randomSemanticText;

0 commit comments

Comments
 (0)