Вот пример массового вызова 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
, означая, что документ с идентификатором 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)
);
Результат тот же, то есть документ с идентификатором 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"
.