Elasticsearch 支持自动重命名别名,请参阅 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" } }
]
}'
与 NEST ,同样可以通过Client.Rename来实现.
事实证明,还可以自动更新别名以指向不同的索引:
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "test1", "alias" : "alias1" } },
{ "add" : { "index" : "test2", "alias" : "alias1" } }
]
}'
有什么方法可以直接在 NEST 中执行后者吗?
现在,我使用的是 Client.RemoveAlias
,然后是 Client.Alias
,它不是原子的。
更新:事实证明,可以通过 client.Raw.IndicesUpdateAliasesPost
发布原始 JSON 来做到这一点,但我仍然想知道是否有更简单的方法。如果没有,我打算自己将其添加到 NEST。
最佳答案
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);
}
关于elasticsearch - NEST - 以原子方式更改别名以指向另一个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537640/