本文介绍了重命名和删除 Elasticsearch 索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using C# .NET application with NEST to create an index.

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

I've created an elasticsearch index that customers can query called index_1. I then build another version of the index using a different instance of the application and call it index_1_temp.

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

What is the safest way for me to rename index_1_temp to index_1 then delete the original index_1?

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

I know ES has aliases but I'm not sure how to use them for this task

原始索引没有关联的别名.

The original index does not have an Alias associated with it.

推荐答案

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

I would recommend always using aliases in scenarios where you may create incrementally differing versions of an index, as may be the case when refining the model of signals within your search strategy.

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

You can add an alias at the point of creating an index

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)
    )
);

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

Then when you create a new index, you can remove the alias from current indices and add it to the new index. This alias swap operation is atomic

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

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

When searching, the consumers would always use the alias. Since the alias points to a single index only, you can also use it to index new documents and update existing documents.

这篇关于重命名和删除 Elasticsearch 索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:12