|
11 | 11 | import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; |
12 | 12 | import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; |
13 | 13 | import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; |
| 14 | +import org.elasticsearch.action.admin.indices.shrink.ResizeType; |
14 | 15 | import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; |
15 | 16 | import org.elasticsearch.action.bulk.BulkRequest; |
16 | 17 | import org.elasticsearch.action.get.GetRequest; |
17 | 18 | import org.elasticsearch.action.index.IndexRequest; |
18 | 19 | import org.elasticsearch.action.search.SearchRequest; |
| 20 | +import org.elasticsearch.action.support.WriteRequest; |
19 | 21 | import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; |
20 | 22 | import org.elasticsearch.cluster.metadata.Template; |
21 | 23 | import org.elasticsearch.common.compress.CompressedXContent; |
|
35 | 37 | import java.util.List; |
36 | 38 | import java.util.UUID; |
37 | 39 |
|
| 40 | +import static org.elasticsearch.index.mapper.DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER; |
| 41 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; |
38 | 42 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; |
39 | 43 | import static org.hamcrest.Matchers.equalTo; |
40 | 44 | import static org.hamcrest.Matchers.is; |
@@ -206,6 +210,38 @@ private void checkIndexSearchAndRetrieval(String dataStreamName, boolean routeOn |
206 | 210 | }); |
207 | 211 | } |
208 | 212 |
|
| 213 | + public void testShrink() throws Exception { |
| 214 | + client().admin() |
| 215 | + .indices() |
| 216 | + .prepareCreate("my-logs") |
| 217 | + .setMapping("@timestamp", "type=date", "host.name", "type=keyword") |
| 218 | + .setSettings(indexSettings(between(3, 5), 0).put("index.mode", "logsdb").put("index.sort.field", "host.name")) |
| 219 | + .get(); |
| 220 | + |
| 221 | + long timestamp = DEFAULT_DATE_TIME_FORMATTER.parseMillis("2025-08-08T00:00:00Z"); |
| 222 | + BulkRequest bulkRequest = new BulkRequest("my-logs"); |
| 223 | + int numDocs = randomIntBetween(100, 10_000); |
| 224 | + for (int i = 0; i < numDocs; i++) { |
| 225 | + timestamp += randomIntBetween(0, 1000); |
| 226 | + String field = "field-" + randomIntBetween(1, 20); |
| 227 | + bulkRequest.add( |
| 228 | + new IndexRequest("my-logs").id(Integer.toString(i)) |
| 229 | + .source("host.name", "host-" + between(1, 5), "@timestamp", timestamp, field, randomNonNegativeLong()) |
| 230 | + ); |
| 231 | + } |
| 232 | + bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE); |
| 233 | + client().bulk(bulkRequest).actionGet(); |
| 234 | + client().admin().indices().prepareFlush("my-logs").get(); |
| 235 | + client().admin().indices().prepareUpdateSettings("my-logs").setSettings(Settings.builder().put("index.blocks.write", true)).get(); |
| 236 | + client().admin() |
| 237 | + .indices() |
| 238 | + .prepareResizeIndex("my-logs", "shrink-my-logs") |
| 239 | + .setResizeType(ResizeType.SHRINK) |
| 240 | + .setSettings(indexSettings(1, 0).build()) |
| 241 | + .get(); |
| 242 | + assertNoFailures(client().admin().indices().prepareForceMerge("shrink-my-logs").setMaxNumSegments(1).setFlush(true).get()); |
| 243 | + } |
| 244 | + |
209 | 245 | static String formatInstant(Instant instant) { |
210 | 246 | return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant); |
211 | 247 | } |
|
0 commit comments