gpt4 book ai didi

elasticsearch - 阻止只读操作的索引,使Elasticsearch处于不一致状态

转载 作者:行者123 更新时间:2023-12-03 02:02:52 25 4
gpt4 key购买 nike

如果要对该索引进行重新索引处理,我想阻止索引进行写操作。
这两个帖子(elasticsearch migrations with c and nestNEST Elasticsearch Reindex examples)都非常有帮助,但是正如this third one所建议的那样,您可能会在此过程的中间丢失更新和删除内容。
我尝试使用elasticsearch中的设置阻止索引进行写操作,但尝试此操作时发现了一些问题。
我使用Nest的UpdateSettings api来更改设置blocks.read_onlyblocks.metadatablocks.readblocks.write:

var client = CreateElasticClient(); // A wrapper method of ElasticClient()
var response = client.UpdateSettings(r => r
.Index(IndexName)
.BlockReadonly()
.BlocksMetadata()
.BlocksRead()
.BlocksWrite()
);

如果我将设置 blocks.read_onlyblocks.metadatablocks.read设置为true(默认设置为值),那么索引将不接受写入操作,但也不允许读取操作,确切地说,我不得不在三种情况下重新安装elasticsearch今天,因为更改这些设置后我什么也做不了。
blocks.write设置为true不会有任何效果,我可以读取索引,但也可以编写索引。
问题
所以,我的问题是,我应该怎么做(在对索引进行重新索引时阻止索引进行写操作)?

最佳答案

我准备了一个简单的例子。也许它将为您的问题锦上添花。

internal class Program
{
private static void Main(string[] args)
{
var indexName = "indexname";
var indexName2 = "indexname2";

var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri).SetDefaultIndex(indexName).EnableTrace();
var client = new ElasticClient(settings);

var indicesResponse = client.DeleteIndex(descriptor => descriptor.Index(indexName));
var indicesResponse2 = client.DeleteIndex(descriptor => descriptor.Index(indexName2));

client.CreateIndex(descriptor => descriptor.Index(indexName).AddMapping<Document>(m => m.MapFromAttributes()));

client.Index(new Document {Id = 1});

client.Refresh();

var acknowledgedResponse = client.UpdateSettings(descriptor => descriptor.Index(indexName).BlocksWrite());

var observable = client.Reindex<Document>(descriptor => descriptor.FromIndex(indexName).ToIndex(indexName2));
observable.Subscribe(new ReindexObserver<Document>(response => Console.WriteLine(response.IsValid),
Console.WriteLine, () => Console.WriteLine("Done")));

var documentToAdd = new Document { Id = 2, Name = "new" };

var indexResponse = client.Index(documentToAdd);

//indexResponse -> can't index new document with message: blocked by: [FORBIDDEN/8/index write (api)];

var getResponse = client.Get<Document>(descriptor => descriptor.Id(1));

//getResponse -> still can read from index

var acknowledgedResponse2 = client.UpdateSettings(descriptor => descriptor.Index(indexName).BlocksWrite(false));

var indexResponse2 = client.Index(documentToAdd);

//indexResponse2 -> now I can add new document to my index

Console.ReadKey();
}
}

public class Document
{
public int Id { get; set; }
public string Name { get; set; }
}

关于elasticsearch - 阻止只读操作的索引,使Elasticsearch处于不一致状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com