Skip to content

Commit 820934c

Browse files
authored
Removing the type from the destination index when using CreateIndexFromSourceAction (#121982) (#122058)
It is possible to create an index in 7.x with a single type. This fixes the CreateIndexFromSourceAction to not copy that type over when creating a destination index from a source index with a type.
1 parent f1f2af7 commit 820934c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

x-pack/plugin/migrate/src/main/java/org/elasticsearch/xpack/migrate/action/CreateIndexFromSourceTransportAction.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ private static Map<String, Object> toMap(@Nullable MappingMetadata sourceMapping
122122
.orElse(Map.of());
123123
}
124124

125+
@SuppressWarnings("unchecked")
125126
private static Map<String, Object> mergeMappings(@Nullable MappingMetadata sourceMapping, Map<String, Object> mappingAddition)
126127
throws IOException {
127128
Map<String, Object> combinedMappingMap = new HashMap<>(toMap(sourceMapping));
128129
XContentHelper.update(combinedMappingMap, mappingAddition, true);
130+
if (sourceMapping != null && combinedMappingMap.size() == 1 && combinedMappingMap.containsKey(sourceMapping.type())) {
131+
combinedMappingMap = (Map<String, Object>) combinedMappingMap.get(sourceMapping.type());
132+
}
129133
return combinedMappingMap;
130134
}
131135

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
import org.apache.http.util.EntityUtils;
1010
import org.elasticsearch.Build;
11+
import org.elasticsearch.TransportVersions;
1112
import org.elasticsearch.Version;
1213
import org.elasticsearch.client.Node;
1314
import org.elasticsearch.client.Request;
15+
import org.elasticsearch.client.RequestOptions;
1416
import org.elasticsearch.client.Response;
1517
import org.elasticsearch.client.RestClient;
1618
import org.elasticsearch.client.RestClientBuilder;
19+
import org.elasticsearch.client.WarningsHandler;
1720
import org.elasticsearch.cluster.metadata.DataStream;
1821
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
1922
import org.elasticsearch.common.settings.SecureString;
@@ -418,8 +421,27 @@ private void createDataStreamFromNonDataStreamIndices(String dataStreamFromNonDa
418421
putIndexTemplateRequest.setJsonEntity(
419422
indexTemplate.replace("$TEMPLATE", templateWithNoTimestamp).replace("$PATTERN", dataStreamFromNonDataStreamIndices + "-*")
420423
);
421-
assertOK(client().performRequest(putIndexTemplateRequest));
422424
String indexName = dataStreamFromNonDataStreamIndices + "-01";
425+
if (minimumTransportVersion().before(TransportVersions.V_8_0_0)) {
426+
/*
427+
* It is not possible to create a 7.x index template with a type. And you can't create an empty index with a type. But you can
428+
* create the index with a type by posting a document to an index with a type. We do that here so that we test that the type is
429+
* removed when we reindex into 8.x.
430+
*/
431+
String typeName = "test-type";
432+
Request createIndexRequest = new Request("POST", indexName + "/" + typeName);
433+
createIndexRequest.setJsonEntity("""
434+
{
435+
"@timestamp": "2099-11-15T13:12:00",
436+
"message": "GET /search HTTP/1.1 200 1070000",
437+
"user": {
438+
"id": "kimchy"
439+
}
440+
}""");
441+
createIndexRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE).build());
442+
assertOK(client().performRequest(createIndexRequest));
443+
}
444+
assertOK(client().performRequest(putIndexTemplateRequest));
423445
bulkLoadDataMissingTimestamp(indexName);
424446
/*
425447
* Next, we will change the index's mapping to include a @timestamp field since we are going to convert it to a data stream. But

0 commit comments

Comments
 (0)