Skip to content

Commit e8f66e5

Browse files
committed
Don't modify source map when parsing composite runtime field (#89114)
When calling RuntimeField.parseRuntimeFields() for fields defined in the search request, we need to wrap the Map containing field definitions in another Map that supports value removal, so that we don't inadvertently remove the definitions from the root request. CompositeRuntimeField was not doing this extra wrapping, which meant that requests that went to multiple shards and that therefore parsed the definitions multiple times would throw an error complaining that the fields parameter was missing, because the root request had been modified.
1 parent 8e9ffd2 commit e8f66e5

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

docs/changelog/89114.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 89114
2+
summary: Don't modify source map when parsing composite runtime field
3+
area: Mapping
4+
type: bug
5+
issues: []
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
setup:
3+
- do:
4+
indices.create:
5+
index: test
6+
body:
7+
settings:
8+
number_of_shards: 2
9+
number_of_replicas: 0
10+
- do:
11+
bulk:
12+
index: test
13+
refresh: true
14+
body: |
15+
{"index":{}}
16+
{"A":2}
17+
18+
---
19+
"search-time composite across multiple shards":
20+
- do:
21+
search:
22+
index: test
23+
body:
24+
query:
25+
term:
26+
"r.shouldReturn" : true
27+
runtime_mappings:
28+
r:
29+
type: composite
30+
fields:
31+
shouldReturn:
32+
type: boolean
33+
script:
34+
source: "emit('shouldReturn',true)"
35+
36+
- match: {hits.total.value: 1}

server/src/main/java/org/elasticsearch/index/mapper/CompositeRuntimeField.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.ArrayList;
1818
import java.util.Collection;
1919
import java.util.Collections;
20+
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.Objects;
@@ -85,7 +86,12 @@ protected RuntimeField createRuntimeField(MappingParserContext parserContext) {
8586
name,
8687
lookup -> factory.newFactory(name, script.get().getParams(), lookup)
8788
);
88-
Map<String, RuntimeField> runtimeFields = RuntimeField.parseRuntimeFields(fields.getValue(), parserContext, builder, false);
89+
Map<String, RuntimeField> runtimeFields = RuntimeField.parseRuntimeFields(
90+
new HashMap<>(fields.getValue()),
91+
parserContext,
92+
builder,
93+
false
94+
);
8995
return new CompositeRuntimeField(name, getParameters(), runtimeFields.values());
9096
}
9197
});
@@ -118,11 +124,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
118124
for (FieldMapper.Parameter<?> parameter : parameters) {
119125
parameter.toXContent(builder, includeDefaults);
120126
}
121-
builder.startObject("fields");
122-
for (RuntimeField subfield : subfields) {
123-
subfield.toXContent(builder, params);
124-
}
125-
builder.endObject();
126127
builder.endObject();
127128
return builder;
128129
}

0 commit comments

Comments
 (0)