如何将相同的文档删除到es索引

2024-04-29 01:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一些文档,我用下面的python代码将它们放在elasticsearch索引上

es.index(index=index_name, doc_type='_doc', body=doc)

如果我运行两次代码,相同的2个文档将出现在这个elasticsearch索引上。 如何消除同一索引上的重复文档? 谢谢你的回答


1条回答
网友
1楼 · 发布于 2024-04-29 01:27:17

您可以设置?op_type=create参数,仅当不存在具有该ID的文档时,才为文档编制索引。例如:

请求1

PUT /customers/_doc/1?op_type=create
{
  "name": "Jane Doe"
}

响应1

{
  "_index" : "customers",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

请求2

PUT /customers/_doc/1?op_type=create
{
  "name": "Jane Doe"
}

响应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
}

相关问题 更多 >