gpt4 book ai didi

elasticsearch - 如何在文档存在时将文档批量插入ElasticSearch而无需更新

转载 作者:行者123 更新时间:2023-12-03 00:39:42 31 4
gpt4 key购买 nike

我正在使用Nest库进行 flex 搜索。我想知道如何在文档存在时不进行更新的情况下将文档批量插入ElasticSearch?

最佳答案

这是将执行创建操作的批量API调用的示例

private static void Main()
{
var defaultIndex = "documents";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);

var client = new ElasticClient(settings);

if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);

client.Index(new MyDocument(1)
{
Message = "new"
}, i => i.Refresh(Refresh.WaitFor));

var documents = new []
{
new MyDocument(1) { Message = "updated" },
new MyDocument(2) { Message = "updated" },
new MyDocument(3) { Message = "updated" },
};

client.Bulk(b => b
.CreateMany(documents)
.Refresh(Refresh.WaitFor)
);

var getResponse = client.Get<MyDocument>(1);

Console.WriteLine(getResponse.Source.Message == "new");
}

public class MyDocument
{
public MyDocument(int id) => Id = id;

public int Id { get; set; }

public string Message { get; set; }
}

输出将为 true,这意味着在批量调用中未创建ID为 1的文档,因为该文档已经存在。如果您查看批量响应,它将是HTTP 200响应,类似于
{
"took" : 1387,
"errors" : true,
"items" : [
{
"create" : {
"_index" : "documents",
"_type" : "mydocument",
"_id" : "1",
"status" : 409,
"error" : {
"type" : "version_conflict_engine_exception",
"reason" : "[mydocument][1]: version conflict, document already exists (current version [1])",
"index_uuid" : "DZIgGMZcSlWRycC1MGhJWQ",
"shard" : "3",
"index" : "documents"
}
}
},
{
"create" : {
"_index" : "documents",
"_type" : "mydocument",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"create" : {
"_index" : "documents",
"_type" : "mydocument",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
}
]
}

重要的是, "errors"true,第一个 "create"操作响应指示错误是什么。

另一种使用 .CreateMany(...)的方法是将 .UpdateMany(...)与upsert操作一起使用,并在文档存在的情况下指定“no-op”操作
client.Bulk(b => b
.UpdateMany(documents, (d, document) => d
.Upsert(document)
.Script(s => s
.Source("ctx.op = 'none'")
)
)
.Refresh(Refresh.WaitFor)
);

结果是相同的,即,具有Id 1的文档不会被覆盖,但是响应会略有不同
{
"took" : 1307,
"errors" : false,
"items" : [
{
"update" : {
"_index" : "documents",
"_type" : "mydocument",
"_id" : "1",
"_version" : 1,
"result" : "noop",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"status" : 200
}
},
{
"update" : {
"_index" : "documents",
"_type" : "mydocument",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"update" : {
"_index" : "documents",
"_type" : "mydocument",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
}
]
}

请注意, "errors"现在是 false,第一个 "update"操作是 "noop"

关于elasticsearch - 如何在文档存在时将文档批量插入ElasticSearch而无需更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50108062/

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