且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重命名和删除弹性搜索索引

更新时间:2023-02-08 19:02:11

我建议始终使用别名,在您可以创建递增不同版本的索引的情况下,可能会在您的搜索策略中改进信号模型的情况。



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

  var client = new ElasticClient(connectionSettings); 

var indexes = new [] {index-v1,index-v2};
var alias =index-alias;

//删除index-v1和index-v2(如果存在)到
//允许此示例重复
foreach(索引中的var index)
{
if(client.IndexExists(index).Exists)
{
client.DeleteIndex(index);
}
}

var createIndexResponse = client.CreateIndex(indices [0],c => c
.Aliases(a => a
。阿利亚斯(别名)

);

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

  createIndexResponse = client.CreateIndex(indices [1]); 

//等待index-v2可操作
var clusterHealthResponse = client.ClusterHealth(c => c
.WaitForStatus(WaitForStatus.Yellow)
。指数(指数为[1]));

//交换别名
var bulkAliasResponse = client.Alias(ba => ba
.Add(add => add.Alias(alias).Index [1]))
.Remove(remove => remove.Alias(alias).Index(*))
);

//验证别名只存在于index-v2
var aliasResponse = client.GetAlias(a => a.Name(alias));

最后一个响应的输出是

  {
index-v2:{
aliases:{
index-alias:{}
}
}
}

搜索时,消费者将始终使用别名。由于别名仅指向单个索引,您还可以使用它来索引新文档并更新现有文档。


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

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.

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

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

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

The output of the last response is

{
  "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.