Skip to content

Commit 1106671

Browse files
committed
Playing with xcontent parsing
1 parent 92d70dc commit 1106671

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
159159
builder.field(INFERENCE_ID_FIELD, inference.inferenceId);
160160
builder.field(MODEL_SETTINGS_FIELD, inference.modelSettings);
161161
if (chunkingSettings != null) {
162-
builder.field(CHUNKING_SETTINGS_FIELD, chunkingSettings);
162+
builder.startObject(CHUNKING_SETTINGS_FIELD);
163+
builder.mapContents(chunkingSettings.asMap());
164+
builder.endObject();
163165
}
164166

165167
if (useLegacyFormat) {
@@ -206,8 +208,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
206208
new ConstructingObjectParser<>(SemanticTextFieldMapper.CONTENT_TYPE, true, (args, context) -> {
207209
List<String> originalValues = (List<String>) args[0];
208210
InferenceResult inferenceResult = (InferenceResult) args[1];
209-
Map<String, Object> chunkingSettingsMap = (Map<String, Object>) args[2];
210-
ChunkingSettings chunkingSettings = chunkingSettingsMap != null ? ChunkingSettingsBuilder.fromMap(chunkingSettingsMap) : null;
211+
ChunkingSettings chunkingSettings = (ChunkingSettings) args[2];
211212
if (context.useLegacyFormat() == false) {
212213
if (originalValues != null && originalValues.isEmpty() == false) {
213214
throw new IllegalArgumentException("Unknown field [" + TEXT_FIELD + "]");
@@ -243,13 +244,13 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
243244
}
244245
);
245246

246-
private static final ConstructingObjectParser<Map<String, Object>, Void> CHUNKING_SETTINGS_PARSER = new ConstructingObjectParser<>(
247+
private static final ConstructingObjectParser<ChunkingSettings, Void> CHUNKING_SETTINGS_PARSER = new ConstructingObjectParser<>(
247248
CHUNKING_SETTINGS_FIELD,
248249
true,
249250
args -> {
250251
@SuppressWarnings("unchecked")
251252
Map<String, Object> map = (Map<String, Object>) args[0];
252-
return map;
253+
return map != null && map.isEmpty() == false ? ChunkingSettingsBuilder.fromMap(map) : null;
253254
}
254255
);
255256

@@ -258,7 +259,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
258259
SEMANTIC_TEXT_FIELD_PARSER.declareObject(constructorArg(), INFERENCE_RESULT_PARSER, new ParseField(INFERENCE_FIELD));
259260
SEMANTIC_TEXT_FIELD_PARSER.declareObjectOrNull(
260261
optionalConstructorArg(),
261-
(p, c) -> CHUNKING_SETTINGS_PARSER.parse(p, null),
262+
(p, c) -> p.map(),
262263
null,
263264
new ParseField(CHUNKING_SETTINGS_FIELD)
264265
);

0 commit comments

Comments
 (0)