重命名和删除 Elasticsearch 索引

Rename and Deleting Elasticsearch Indexes

我正在使用带有 NEST 的 C# .NET 应用程序来创建索引。

我创建了一个名为 index_1 的客户可以查询的弹性搜索索引。然后,我使用应用程序的不同实例构建了另一个版本的索引,并将其命名为 index_1_temp.

我将 index_1_temp 重命名为 index_1 然后删除原来的 index_1 最安全的方法是什么?

我知道 ES 有别名,但我不确定如何将它们用于此任务

编辑:原始索引没有关联的别名。

我总是建议 using aliases 在您可能创建索引的增量不同版本的情况下,例如在您的搜索策略中优化信号模型时的情况。

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

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" : { }
    }
  }
}

消费者在搜索时,总是会使用别名。由于别名仅指向单个索引,您还可以使用它来索引新文档和更新现有文档。