You can set the ?op_type=create parameter to index a document only if no document with that ID exists. For example:
Request 1
PUT /customers/_doc/1?op_type=create
{
"name": "Jane Doe"
}
Response 1
{
"_index" : "customers",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
Request 2
PUT /customers/_doc/1?op_type=create
{
"name": "Jane Doe"
}
Response 2
{
"error" : {
"root_cause" : [
{
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "5Oar4GFhQjq_EM88qqd6PA",
"shard" : "0",
"index" : "customers"
}
],
"type" : "version_conflict_engine_exception",
"reason" : "[1]: version conflict, document already exists (current version [1])",
"index_uuid" : "5Oar4GFhQjq_EM88qqd6PA",
"shard" : "0",
"index" : "customers"
},
"status" : 409
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…