diff --git a/docs/changelog/89114.yaml b/docs/changelog/89114.yaml new file mode 100644 index 0000000000000..c363437586249 --- /dev/null +++ b/docs/changelog/89114.yaml @@ -0,0 +1,5 @@ +pr: 89114 +summary: Don't modify source map when parsing composite runtime field +area: Mapping +type: bug +issues: [] diff --git a/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/111_search_time_composite.yml b/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/111_search_time_composite.yml new file mode 100644 index 0000000000000..dc10d16c26ca3 --- /dev/null +++ b/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/111_search_time_composite.yml @@ -0,0 +1,36 @@ +--- +setup: + - do: + indices.create: + index: test + body: + settings: + number_of_shards: 2 + number_of_replicas: 0 + - do: + bulk: + index: test + refresh: true + body: | + {"index":{}} + {"A":2} + +--- +"search-time composite across multiple shards": + - do: + search: + index: test + body: + query: + term: + "r.shouldReturn" : true + runtime_mappings: + r: + type: composite + fields: + shouldReturn: + type: boolean + script: + source: "emit('shouldReturn',true)" + + - match: {hits.total.value: 1} diff --git a/server/src/main/java/org/elasticsearch/index/mapper/CompositeRuntimeField.java b/server/src/main/java/org/elasticsearch/index/mapper/CompositeRuntimeField.java index a8e9b487160de..ea8fbb0dcd60b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/CompositeRuntimeField.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/CompositeRuntimeField.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -85,7 +86,12 @@ protected RuntimeField createRuntimeField(MappingParserContext parserContext) { name, lookup -> factory.newFactory(name, script.get().getParams(), lookup) ); - Map runtimeFields = RuntimeField.parseRuntimeFields(fields.getValue(), parserContext, builder, false); + Map runtimeFields = RuntimeField.parseRuntimeFields( + new HashMap<>(fields.getValue()), + parserContext, + builder, + false + ); return new CompositeRuntimeField(name, getParameters(), runtimeFields.values()); } }); @@ -118,11 +124,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws for (FieldMapper.Parameter parameter : parameters) { parameter.toXContent(builder, includeDefaults); } - builder.startObject("fields"); - for (RuntimeField subfield : subfields) { - subfield.toXContent(builder, params); - } - builder.endObject(); builder.endObject(); return builder; }