您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

重命名和删除Elasticsearch索引

重命名和删除Elasticsearch索引

我建议在可能创建增量不同版本的索引的方案中始终使用别名,例如在搜索策略中优化信号模型时可能就是这种情况。

您可以在创建索引时添加别名

var client = new ElasticClient(connectionSettings);

var indices = new[] { "index-v1", "index-v2" };
var alias = "index-alias";

// delete index-v1 and index-v2 if they exist, to 
// allow this example to be repeatable
foreach (var index in indices)
{
    if (client.IndexExists(index).Exists)
    {
        client.DeleteIndex(index);
    }
}

var createIndexResponse = client.CreateIndex(indices[0], c => c
    .Aliases(a => a
        .Alias(alias)
    )
);

然后,当您创建新索引时,可以从当前索引中删除别名并将其添加到新索引中。此别名交换操作是原子的

createIndexResponse = client.CreateIndex(indices[1]);

// wait for index-v2 to be operable
var clusterHealthResponse = client.ClusterHealth(c => c
    .WaitForStatus(WaitForStatus.Yellow)
    .Index(indices[1]));

// swap the alias
var bulkAliasResponse = client.Alias(ba => ba
    .Add(add => add.Alias(alias).Index(indices[1]))
    .Remove(remove => remove.Alias(alias).Index("*"))
);

// verify that the alias only exists on index-v2
var aliasResponse = client.GetAlias(a => a.Name(alias));

最后一个响应的输出

{
  "index-v2" : {
    "aliases" : {
      "index-alias" : { }
    }
  }
}

搜索时,使用者将始终使用别名。由于别名仅指向单个索引,因此您也可以使用别名为新文档建立索引并更新现有文档。

其他 2022/1/1 18:22:05 有514人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶