|  | 
| 13 | 13 | import org.elasticsearch.action.admin.indices.diskusage.TransportAnalyzeIndexDiskUsageAction; | 
| 14 | 14 | import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest; | 
| 15 | 15 | import org.elasticsearch.action.admin.indices.get.GetIndexRequest; | 
|  | 16 | +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; | 
| 16 | 17 | import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; | 
| 17 | 18 | import org.elasticsearch.action.admin.indices.rollover.RolloverRequest; | 
| 18 | 19 | import org.elasticsearch.action.admin.indices.segments.IndicesSegmentsRequest; | 
| 19 | 20 | import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequest; | 
| 20 | 21 | import org.elasticsearch.action.admin.indices.template.put.PutComponentTemplateAction; | 
| 21 | 22 | import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; | 
| 22 | 23 | import org.elasticsearch.action.bulk.BulkRequest; | 
|  | 24 | +import org.elasticsearch.action.bulk.BulkRequestBuilder; | 
|  | 25 | +import org.elasticsearch.action.bulk.BulkResponse; | 
| 23 | 26 | import org.elasticsearch.action.bulk.IndexDocFailureStoreStatus; | 
| 24 | 27 | import org.elasticsearch.action.get.GetRequest; | 
| 25 | 28 | import org.elasticsearch.action.index.IndexRequest; | 
| 26 | 29 | import org.elasticsearch.action.search.SearchRequest; | 
| 27 | 30 | import org.elasticsearch.action.support.WriteRequest; | 
| 28 | 31 | import org.elasticsearch.cluster.metadata.ComponentTemplate; | 
| 29 | 32 | import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | 
|  | 33 | +import org.elasticsearch.cluster.metadata.IndexMetadata; | 
| 30 | 34 | import org.elasticsearch.cluster.metadata.Template; | 
| 31 | 35 | import org.elasticsearch.common.Strings; | 
| 32 | 36 | import org.elasticsearch.common.compress.CompressedXContent; | 
| 33 | 37 | import org.elasticsearch.common.settings.Settings; | 
| 34 | 38 | import org.elasticsearch.common.time.DateFormatter; | 
| 35 | 39 | import org.elasticsearch.common.time.FormatNames; | 
| 36 | 40 | import org.elasticsearch.common.xcontent.XContentHelper; | 
|  | 41 | +import org.elasticsearch.index.IndexMode; | 
| 37 | 42 | import org.elasticsearch.index.IndexSettings; | 
| 38 | 43 | import org.elasticsearch.index.query.RangeQueryBuilder; | 
|  | 44 | +import org.elasticsearch.index.reindex.BulkByScrollResponse; | 
|  | 45 | +import org.elasticsearch.index.reindex.ReindexAction; | 
|  | 46 | +import org.elasticsearch.index.reindex.ReindexRequest; | 
| 39 | 47 | import org.elasticsearch.indices.InvalidIndexTemplateException; | 
| 40 | 48 | import org.elasticsearch.plugins.Plugin; | 
|  | 49 | +import org.elasticsearch.reindex.ReindexPlugin; | 
| 41 | 50 | import org.elasticsearch.rest.RestStatus; | 
| 42 | 51 | import org.elasticsearch.search.builder.SearchSourceBuilder; | 
| 43 | 52 | import org.elasticsearch.test.ESSingleNodeTestCase; | 
|  | 
| 53 | 62 | 
 | 
| 54 | 63 | import static org.elasticsearch.test.MapMatcher.assertMap; | 
| 55 | 64 | import static org.elasticsearch.test.MapMatcher.matchesMap; | 
|  | 65 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | 
| 56 | 66 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; | 
| 57 | 67 | import static org.hamcrest.Matchers.containsString; | 
| 58 | 68 | import static org.hamcrest.Matchers.equalTo; | 
| @@ -98,7 +108,7 @@ public class TSDBIndexingIT extends ESSingleNodeTestCase { | 
| 98 | 108 | 
 | 
| 99 | 109 |     @Override | 
| 100 | 110 |     protected Collection<Class<? extends Plugin>> getPlugins() { | 
| 101 |  | -        return List.of(DataStreamsPlugin.class, InternalSettingsPlugin.class); | 
|  | 111 | +        return List.of(DataStreamsPlugin.class, InternalSettingsPlugin.class, ReindexPlugin.class); | 
| 102 | 112 |     } | 
| 103 | 113 | 
 | 
| 104 | 114 |     @Override | 
| @@ -557,6 +567,60 @@ public void testTrimId() throws Exception { | 
| 557 | 567 |         }); | 
| 558 | 568 |     } | 
| 559 | 569 | 
 | 
|  | 570 | +    public void testReindexing() throws Exception { | 
|  | 571 | +        String dataStreamName = "my-ds"; | 
|  | 572 | +        String reindexedDataStreamName = "my-reindexed-ds"; | 
|  | 573 | +        var putTemplateRequest = new TransportPutComposableIndexTemplateAction.Request("id"); | 
|  | 574 | +        putTemplateRequest.indexTemplate( | 
|  | 575 | +            ComposableIndexTemplate.builder() | 
|  | 576 | +                .indexPatterns(List.of(dataStreamName, reindexedDataStreamName)) | 
|  | 577 | +                .template( | 
|  | 578 | +                    new Template( | 
|  | 579 | +                        Settings.builder().put("index.mode", "time_series").build(), | 
|  | 580 | +                        new CompressedXContent(MAPPING_TEMPLATE), | 
|  | 581 | +                        null | 
|  | 582 | +                    ) | 
|  | 583 | +                ) | 
|  | 584 | +                .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate(false, false)) | 
|  | 585 | +                .build() | 
|  | 586 | +        ); | 
|  | 587 | +        assertAcked(client().execute(TransportPutComposableIndexTemplateAction.TYPE, putTemplateRequest)); | 
|  | 588 | + | 
|  | 589 | +        // index doc | 
|  | 590 | +        long docCount = randomLongBetween(10, 50); | 
|  | 591 | +        Instant startTime = Instant.now(); | 
|  | 592 | +        BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); | 
|  | 593 | +        bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); | 
|  | 594 | +        for (int i = 0; i < docCount; i++) { | 
|  | 595 | +            IndexRequest indexRequest = new IndexRequest(dataStreamName).opType(DocWriteRequest.OpType.CREATE); | 
|  | 596 | +            indexRequest.source(DOC.replace("$time", formatInstant(startTime.plusSeconds(i))), XContentType.JSON); | 
|  | 597 | +            bulkRequestBuilder.add(indexRequest); | 
|  | 598 | +        } | 
|  | 599 | +        BulkResponse bulkResponse = bulkRequestBuilder.get(); | 
|  | 600 | +        assertThat(bulkResponse.hasFailures(), is(false)); | 
|  | 601 | + | 
|  | 602 | +        BulkByScrollResponse reindexResponse = safeGet( | 
|  | 603 | +            client().execute( | 
|  | 604 | +                ReindexAction.INSTANCE, | 
|  | 605 | +                new ReindexRequest().setSourceIndices(dataStreamName).setDestIndex(reindexedDataStreamName).setDestOpType("create") | 
|  | 606 | +            ) | 
|  | 607 | +        ); | 
|  | 608 | +        assertThat(reindexResponse.getCreated(), equalTo(docCount)); | 
|  | 609 | + | 
|  | 610 | +        GetIndexResponse getIndexResponse = safeGet( | 
|  | 611 | +            indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(dataStreamName, reindexedDataStreamName)) | 
|  | 612 | +        ); | 
|  | 613 | +        assertThat(getIndexResponse.getIndices().length, equalTo(2)); | 
|  | 614 | +        var index1 = getIndexResponse.getIndices()[0]; | 
|  | 615 | +        var index2 = getIndexResponse.getIndices()[1]; | 
|  | 616 | +        assertThat(getIndexResponse.getSetting(index1, IndexSettings.MODE.getKey()), equalTo(IndexMode.TIME_SERIES.getName())); | 
|  | 617 | +        assertThat(getIndexResponse.getSetting(index2, IndexSettings.MODE.getKey()), equalTo(IndexMode.TIME_SERIES.getName())); | 
|  | 618 | +        assertThat( | 
|  | 619 | +            getIndexResponse.getSetting(index2, IndexMetadata.INDEX_ROUTING_PATH.getKey()), | 
|  | 620 | +            equalTo(getIndexResponse.getSetting(index1, IndexMetadata.INDEX_ROUTING_PATH.getKey())) | 
|  | 621 | +        ); | 
|  | 622 | +    } | 
|  | 623 | + | 
| 560 | 624 |     static String formatInstant(Instant instant) { | 
| 561 | 625 |         return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant); | 
| 562 | 626 |     } | 
|  | 
0 commit comments