【问题标题】:Rename and Deleting Elasticsearch Indexes重命名和删除 Elasticsearch 索引
【发布时间】:2017-05-23 20:10:32
【问题描述】:

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

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

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

我知道 ES 有别名,但我不知道如何在这个任务中使用它们

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

【问题讨论】:

  • 我刚刚在 NEST 中阅读了有关 Client.Swap 的信息,但我使用的版本没有(我相信它已经贬值了)

标签: elasticsearch rename alias nest


【解决方案1】:

在您可能创建增量不同版本的索引的情况下,我建议始终使用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" : { }
    }
  }
}

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

【讨论】:

  • 嗨,拉斯。感谢您的详细回复,我可能会实施一个版本。我遇到的唯一棘手的问题是索引已经创建(没有别名),因此所有进入索引的请求都只使用索引名称。我想我可以创建一个新别名,将其添加到原始索引中,然后重写代码,以便查询索引引用别名而不是索引名称?
  • 是的,这听起来是一种合理的方法。然后,您可以灵活地更改别名指向的 ind[ex/ices]
猜你喜欢
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 2020-05-08
相关资源
最近更新 更多