我正在使用带有NEST的C#.NET应用程序来创建索引。
我创建了一个Elasticsearch索引,客户可以查询该索引,称为index_1。然后,我使用应用程序的不同实例构建索引的另一个版本,并将其称为index_1_temp。
我将index_1_temp重命名为index_1然后删除原始index_1的最安全方法是什么?
我知道ES具有别名,但是我不确定如何将其用于此任务
编辑:原始索引没有与之关联的别名。
我建议在可能创建增量不同版本的索引的方案中始终使用别名,例如在搜索策略中优化信号模型时可能就是这种情况。
您可以在创建索引时添加别名
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" : { } } } }
搜索时,使用者将始终使用别名。由于别名仅指向单个索引,因此您也可以使用别名为新文档建立索引并更新现有文档。