The NEST client currently supports this functionality. Please use the Client.Swap
method.
NEST - Atomically change an alias to point to another index
-
18-06-2023 - |
Вопрос
Elasticsearch supports renaming an alias atomically, see here:
Renaming an alias is a simple remove then add operation within the same API. This operation is atomic, no need to worry about a short period of time where the alias does not point to an index.
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test1", "alias" : "alias2" } }
]
}'
With NEST, the same can be achieved via Client.Rename.
Turns out, it's also possible to atomically update an alias to point to a different index:
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}'
Any way to do the latter directly in NEST?
Right now, I'm using Client.RemoveAlias
followed by Client.Alias
which is not atomic.
UPDATE: turns out, it's possible to do this by POSTing raw JSON via client.Raw.IndicesUpdateAliasesPost
, but I still wonder if there's an easier way. If not, I'm planning to add this to NEST myself.
Решение 2
Другие советы
private void SwapAliases(IElasticClient client, string sourceIndexName, string destinationIndexName)
{
var aliasNames = client.GetAliasesPointingToIndex(sourceIndexName).Select(a => a.Name);
var bulkAliasDescriptor = new BulkAliasDescriptor();
foreach (var aliasName in aliasNames)
{
// Remove the alias from the source index
bulkAliasDescriptor.Remove(a => a.Index(sourceIndexName).Alias(aliasName));
// Add the alias to the destination index
bulkAliasDescriptor.Add(a => a.Index(destinationIndexName).Alias(aliasName));
}
// Execute the alias swap
var response = client.Alias(bulkAliasDescriptor);
}