使用python在Elasticseach中批量部分Upsert

L-R*_*L-R 11 python upsert elasticsearch

我想向ES发送n个upsert部分请求,这样的事情可能吗?因此,如果文档不存在,请插入我的部分文档.如果它已存在,请使用部分doc更新它.

使用批量助手,我尝试了很多变化,但它们都消除了现有的价值,转而支持新的价值.

data = [{
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "doc": {"newkey": 'newvalue'}
}]
helpers.bulk(es, data, index='my_index', doc_type='my_type')
Run Code Online (Sandbox Code Playgroud)

要么

data = [{
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "_source": {"newkey": 'newvalue'}
}]
helpers.bulk(es, data, index='my_index', doc_type='my_type')
Run Code Online (Sandbox Code Playgroud)

也不起作用.

小智 10

正如J. Ku回答的那样,他给出了正确答案,但提供的代码不完整.所以发布完整的答案.

data = [{
    "_op_type": 'update',
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "doc": {"newkey": 'newvalue'},
    "doc_as_upsert":True
}]

helpers.bulk(es, data, index='my_index', doc_type='my_type')
Run Code Online (Sandbox Code Playgroud)


J.K*_*.Ku 1

我认为您需要包含文档中提到的操作并将 upsert 设置为 true

data = [{
    "_op_type": 'update',
    "_index": 'my_index',
    "_type": 'my_type',
    "_id": 12345,
    "doc": {"newkey": 'newvalue'}
}]
helpers.bulk(es, data, index='my_index', doc_type='my_type')
Run Code Online (Sandbox Code Playgroud)