sure... first, you can read here and here about aliases.
In short, you have an API that enables you to create/update aliases to indices. At the moment, there's nothing in elasticsearch that will automatically do that for you, so you need to make sure to call these APIs at the right times. For example, you can update an alias that points to the last two indices (assuming daily indices), by removing the index that is associated with two days ago, and adding a new index that is associated with today:
POST _aliases
{
"actions" : [
{ "remove" : { "index" : "logstash-2015.06.06", "alias" : "last_two_days" } },
{ "add" : { "index" : "logstash-2015.06.08", "alias" : "last_two_days" } },
]
}
Calling this every midnight (again, assuming daily indices), the last_two_days
alias will always point to the two indices representing the last two days. Now... here's the cool part, normally I'd tell you use curator or to run a cron job that does this automatically. But why use cron if you already have watcher... here's a watch that will take care of it for you:
PUT _watcher/watch/roll-logstash-indices-aliases
{
"metadata" : {
"index_pattern" : "'logstash-'YYYY.MM.dd",
"rollover" : "1d"
},
"trigger" : {
"schedule" : { "daily" : { "at" : "midnight" }}
},
"transform" : {
"script" : "def rolloverMillis = org.elasticsearch.common.unit.TimeValue.parseTimeValue(ctx.metadata.rollover, null).millis(); def pattern = org.elasticsearch.common.joda.time.format.DateTimeFormat.forPattern(ctx.metadata.index_pattern); return [ 'rollover_millis' : rolloverMillis, 'remove-index' : pattern.print(ctx.trigger.scheduled_time.minus(rolloverMillis * 2 + 1000)), 'add-index' : pattern.print(ctx.trigger.scheduled_time)];"
},
"actions" : {
"update-alias" : {
"webhook" : {
"method" : "POST",
"host" : "localhost",
"port" : 9200,
"path" : "/_aliases",
"body" : {
"inline" : {
"actions" : [
{ "remove" : { "index" : "{{ctx.payload.remove-index}}", "alias" : "last_two_indices" } },
{ "add" : { "index" : "{{ctx.payload.add-index}}", "alias" : "last_two_indices" } }
]
}
}
}
}
}
}
In the above watch, I defined the rolling interval and the logstash index name pattern in the metadata (so if you plan to change the rolling nature of the indices, these along with the schedule are the only parameters you'll need to change)... currently the rollover is set to 1 day and the index name pattern is set appropriately (so it'll change daily to indicate the associated date).
The transform takes care of computing the names of the indices that need to be deleted and added.
The webhook action calls the elasticsearch aliases API to add/remove the appropriate indices.
The schedule is set to run every midnight (appropriate for daily rollover).
hope it helps