Skip to content

Commit 8bd2d8d

Browse files
committed
Make sure to nest mappings under the type name when communicating with pre-6.6 nodes.
1 parent 8baf144 commit 8bd2d8d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.elasticsearch.common.Strings;
3434
import org.elasticsearch.common.bytes.BytesArray;
3535
import org.elasticsearch.common.bytes.BytesReference;
36+
import org.elasticsearch.common.collect.MapBuilder;
3637
import org.elasticsearch.common.io.stream.StreamInput;
3738
import org.elasticsearch.common.io.stream.StreamOutput;
3839
import org.elasticsearch.common.settings.Settings;
@@ -491,8 +492,24 @@ public void writeTo(StreamOutput out) throws IOException {
491492
writeSettingsToStream(settings, out);
492493
out.writeVInt(mappings.size());
493494
for (Map.Entry<String, String> entry : mappings.entrySet()) {
494-
out.writeString(entry.getKey());
495-
out.writeString(entry.getValue());
495+
String type = entry.getKey();
496+
String value = entry.getValue();
497+
498+
out.writeString(type);
499+
if (out.getVersion().onOrAfter(Version.V_6_6_0)) {
500+
out.writeString(value);
501+
} else {
502+
// Versions before 6.6.0 are missing a bug fix around empty mappings that are not nested under
503+
// the type name. We therefore nest them under the type name before sending them to these nodes.
504+
Map<String, Object> mappingSource = XContentHelper.convertToMap(
505+
new BytesArray(entry.getValue()), false, XContentType.JSON).v2();
506+
if (mappingSource.size() != 1 || !mappingSource.containsKey(entry.getKey())) {
507+
mappingSource = MapBuilder.<String, Object>newMapBuilder().put(type, mappingSource).map();
508+
}
509+
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
510+
builder.map(mappingSource);
511+
out.writeString(Strings.toString(builder));
512+
}
496513
}
497514
if (out.getVersion().before(Version.V_6_5_0)) {
498515
// Size of custom index metadata, which is removed

0 commit comments

Comments
 (0)