【发布时间】:2018-04-30 20:52:17
【问题描述】:
我在 Nest 库中使用弹性搜索。我想知道如何在文档存在时将文档批量插入到 ElasticSearch 而不更新?
【问题讨论】:
标签: c# elasticsearch nest
我在 Nest 库中使用弹性搜索。我想知道如何在文档存在时将文档批量插入到 ElasticSearch 而不更新?
【问题讨论】:
标签: c# elasticsearch nest
这是一个执行创建操作的批量 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 的文档未在批量调用中创建,因为它已经存在。如果你看一下批量响应,它会是一个类似于
{
"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 操作一起使用,在文档存在的情况下指定“无操作”操作
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"。
【讨论】: