如何配置Elasticsearch实现按上传日期生成可删除的索引别名
Alright, let's tackle your requirement step by step. You want to generate date-suffixed indexes (like myindex-2024.05.20) automatically when uploading data daily, tie all these indexes to the same ILM policy for easy deletion, and use aliases for seamless writing/querying. Here's how to adjust your existing setup to make this work:
Option 1: Auto-create date-named indexes daily (no rollover needed)
This approach directly creates a new index for each calendar day, with no dependency on data size. All indexes will be managed by your ILM policy and can be queried through a unified alias.
1. Update your ILM policy (simplify for daily indexes)
Since we're creating indexes per day, we don't need the rollover action in the hot phase anymore—just keep the delete phase to auto-purge old data:
PUT _ilm/policy/my_policy { "policy": { "phases": { "hot": { "actions": {} }, "delete": { "min_age": "180d", "actions": { "delete": {} } } } } }
2. Create an index template for daily indexes
This template ensures every new date-suffixed index inherits your settings, ILM policy, and is added to a query alias:
PUT _template/my_daily_index_template { "index_patterns": ["myindex-*"], "settings": { "number_of_shards": 1, "number_of_replicas": 1, "index.lifecycle.name": "my_policy", "index.lifecycle.origination_date": "{{now/d}}" // Use today's date to calculate ILM age correctly }, "aliases": { "myindex-all": {} // Alias to query all historical daily indexes } }
3. Write data to the daily index in Java
Use Elasticsearch's date math syntax or directly generate the daily index name in your code to auto-create the day's index on first write:
// Option 1: Use Elasticsearch date math (auto-resolves to today's index) String indexName = "<myindex-{now/d}>"; // Option 2: Generate the date-suffixed index name manually String formattedDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd")); String indexName = "myindex-" + formattedDate; // Then perform your index request as usual IndexRequest request = new IndexRequest(indexName); request.id("unique-id-1"); request.source("field1", "value1", "field2", "value2", XContentType.JSON); client.index(request, RequestOptions.DEFAULT);
Option 2: Use ILM Rollover for daily + size-based index creation
If you want to keep the flexibility of rolling over indexes when they hit your 25GB limit (even within a single day) while still using date-based naming, use this setup:
1. Update ILM policy to trigger rollover on size OR age
This ensures a new index is created either when it hits 25GB or at the end of the day:
PUT _ilm/policy/my_policy { "policy": { "phases": { "hot": { "actions": { "rollover": { "max_size": "25GB", "max_age": "1d" } } }, "delete": { "min_age": "180d", "actions": { "delete": {} } } } } }
2. Create an initial rollover index
Start with a date-prefixed index, link it to your write alias, and configure rollover settings:
PUT myindex-2024.05.20-000001 { "aliases": { "myindex-write": { "is_write_index": true } }, "settings": { "number_of_shards": 1, "number_of_replicas": 1, "index.lifecycle.name": "my_policy", "index.lifecycle.rollover_alias": "myindex-write", "index.lifecycle.origination_date": "{{now/d}}" } }
3. Create a template for rollover indexes
Ensure all future rolled-over indexes inherit your settings and join the query alias:
PUT _template/my_rollover_template { "index_patterns": ["myindex-*-*"], "settings": { "number_of_shards": 1, "number_of_replicas": 1, "index.lifecycle.name": "my_policy", "index.lifecycle.rollover_alias": "myindex-write" }, "aliases": { "myindex-all": {} // Unified alias for querying all rolled-over indexes } }
4. Write data via the write alias
In your Java code, just write to the myindex-write alias—ILM will automatically handle switching to the new index when rollover conditions are met:
IndexRequest request = new IndexRequest("myindex-write"); request.id("unique-id-1"); request.source("field1", "value1", XContentType.JSON); client.index(request, RequestOptions.DEFAULT);
Managing Deletions
- Manual delete a single daily index: Run
DELETE myindex-2024.05.20 - Auto-delete old indexes: Your ILM policy will automatically delete indexes older than 180 days
- Delete all indexes matching the pattern:
DELETE myindex-*(use this cautiously!)
内容的提问来源于stack exchange,提问作者GKr297




