elasticsearch - NEST - 以原子方式更改别名以指向另一个索引

标签 elasticsearch nest

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/

相关文章:

elasticsearch - 列表中的JSON如何匹配?

java - 如何使用java查询具有特定需要字段的elasticsearch

nest - 如何让 NEST 与 Proxy 一起使用,例如 Fiddler

elasticsearch - NEST Elasticsearch 错误 : The underlying connection was closed

elasticsearch - NEST:如何处理包含单个值的数组字段(Newtonsoft Json 序列化错误)

c# - 在 ElasticSearch 中使用 Bulk.IndexMany 指定 _id 字段

c# - 索引未分析的映射无法使用嵌套

python - 如何在Dask中写入Elastic db?

elasticsearch - 无法开启Elasticsearch动态映射

c# - 我如何使用C#nest解决 Elasticsearch 中的土耳其字母问题?