From 6440373c4c9ac115af76cc87d6119be1e7630c6a Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Mon, 7 Jun 2021 10:18:14 +0200 Subject: [PATCH] wip --- .../index/mapper/ObjectRuntimeField.java | 45 ++++++++----------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ObjectRuntimeField.java b/server/src/main/java/org/elasticsearch/index/mapper/ObjectRuntimeField.java index a77e04d721fb6..95cbbd0c50369 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ObjectRuntimeField.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ObjectRuntimeField.java @@ -16,18 +16,19 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.function.Function; +import java.util.stream.Collectors; /** * A runtime field of type object. Defines a script at the top level, which emits multiple sub-fields. * The sub-fields are declared within the object in order to be made available to the field_caps and search API. */ //TODO find a better name!? -public class ObjectRuntimeField implements RuntimeField, DynamicFieldType { +public class ObjectRuntimeField implements RuntimeField { public static final Parser PARSER = new Parser(name -> new RuntimeField.Builder(name) { @@ -68,28 +69,32 @@ protected RuntimeField createRuntimeField(Mapper.TypeParser.ParserContext parser // only print out their leaf field name (which may contain dots!) Map runtimeFields = RuntimeField.parseRuntimeFields(fields.getValue(), parserContext, searchLookup -> factory.newFactory(name, script.get().getParams(), searchLookup), false); - return new ObjectRuntimeField(name, runtimeFields, this); + return new ObjectRuntimeField(name, runtimeFields.values(), this); } }); private final String name; - private final Map subfields; + private final Collection subfields; private final ToXContent toXContent; - ObjectRuntimeField(String name, Map subfields, ToXContent toXContent) { + ObjectRuntimeField(String name, Collection subfields, ToXContent toXContent) { this.name = name; - this.subfields = subfields; - this.toXContent = toXContent; + this.subfields = subfields.stream().flatMap(runtimeField -> runtimeField.asMappedFieldTypes().stream()) + .collect(Collectors.toList()); + this.toXContent = (builder, params) -> { + toXContent.toXContent(builder, params); + builder.startObject("fields2"); + for (RuntimeField runtimeField : subfields) { + runtimeField.toXContent(builder, params); + } + builder.endObject(); + return builder; + }; } @Override public void doXContentBody(XContentBuilder builder, Params params) throws IOException { toXContent.toXContent(builder, params); - builder.startObject("fields2"); - for (RuntimeField runtimeField : subfields.values()) { - runtimeField.toXContent(builder, params); - } - builder.endObject(); } @Override @@ -103,8 +108,8 @@ public String typeName() { } @Override - public MappedFieldType asMappedFieldType() { - return null; + public Collection asMappedFieldTypes() { + return subfields; } private static Map parseFields(String name, Object fieldsObject) { @@ -116,16 +121,4 @@ private static Map parseFields(String name, Object fieldsObject) Map fields = (Map) fieldsObject; return fields; } - - @Override - public MappedFieldType getChildFieldType(String path) { - RuntimeField runtimeField = subfields.get(path); - //we could create a KeywordScriptFieldType on-the-fly for any unmapped field, but they would not be exposed to field_caps - return runtimeField == null ? null : runtimeField.asMappedFieldType(); - } - - @Override - public Set getKnownSubfields() { - return subfields.keySet(); - } }