仅当 doc 不存在时,Elasticsearch 中的批量索引

bulk index in Elasticsearch only if doc is absent

使用 elasticsearch bulk api 可以按如下方式索引文档:

 for doc in shingles:
            actions.append({
                "_op_type": "index",
                "_index": index_name,
                '_id': hashed_id,
                "content_completion": {
                    "input": [doc],
                    "weight": 1
                }
            })
    helpers.bulk(self.es, actions)

但我需要的是仅索引数据,如果索引中不存在文档。如何使用批量 API?

实现此目的

索引文档时弹性搜索中有一个选项

如果您设置 op_type=create 那么它只会创建不存在的文档,否则它将失败并且这些文档 return 错误

POST _bulk/
{ "index" : { "_index" : "test", "_id" : "1" ,**"op_type":"create"** } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

这是相同的link。