使用python在Elasticseach中大容量部分向上插入

2024-05-16 18:51:08 发布

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

我想向ES发送n个upsert部分请求,这样做可能吗? 因此,如果文档不存在,请插入我的部分文档。如果已经存在,用分部单据更新。在

使用bulkhelpers,我尝试了大量的变体,但是它们都会抹去现有的值,转而使用新的值。在

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

或者

^{pr2}$

也不起作用。在


Tags: 文档iddataindexdocesmytype
2条回答

我认为您需要包括documentation中提到的操作,并将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')

正如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')

相关问题 更多 >