Skip to content

Commit 3a52906

Browse files
authored
Removing the type from the destination index when using CreateIndexFromSourceAction (elastic#121982) (elastic#122059)
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 ba9b6d7 commit 3a52906

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import org.elasticsearch.Version;
1313
import org.elasticsearch.client.Node;
1414
import org.elasticsearch.client.Request;
15+
import org.elasticsearch.client.RequestOptions;
1516
import org.elasticsearch.client.Response;
1617
import org.elasticsearch.client.RestClient;
1718
import org.elasticsearch.client.RestClientBuilder;
19+
import org.elasticsearch.client.WarningsHandler;
1820
import org.elasticsearch.cluster.metadata.DataStream;
1921
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
2022
import org.elasticsearch.common.settings.SecureString;
@@ -419,8 +421,27 @@ private void createDataStreamFromNonDataStreamIndices(String dataStreamFromNonDa
419421
putIndexTemplateRequest.setJsonEntity(
420422
indexTemplate.replace("$TEMPLATE", templateWithNoTimestamp).replace("$PATTERN", dataStreamFromNonDataStreamIndices + "-*")
421423
);
422-
assertOK(client().performRequest(putIndexTemplateRequest));
423424
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));
424445
bulkLoadDataMissingTimestamp(indexName);
425446
/*
426447
* 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)