重命名和删除Elasticsearch索引

Cor*_*iew 3 alias rename elasticsearch nest

我在NEST中使用C#.NET应用程序来创建索引.

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

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

我知道ES有别名,但我不知道如何使用它们来完成这项任务

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

Rus*_*Cam 6

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

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

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)
    )
);
Run Code Online (Sandbox Code Playgroud)

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

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));
Run Code Online (Sandbox Code Playgroud)

最后一个响应的输出是

{
  "index-v2" : {
    "aliases" : {
      "index-alias" : { }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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