使用Python的弹性搜索进行批量更新

Obj*_*_88 8 python elasticsearch elasticsearch-py

我正在尝试基于文档属性的状态更改进行批量更新.创建工作正常,但批量是吓坏了.我收到"脚本或文档丢失"效果的错误,但一切看起来都不错.

以下是我尝试批量更新的方法:

frequency_cleared = [
    {
        "_id": result['_id'], 
        "_type": "the-type", 
        "_index": "the-index", 
        "_source": result['_source'],
        "_op_type": 'update'
    } 
    for result in search_results['hits']['hits']
]
Run Code Online (Sandbox Code Playgroud)

我在迭代结果的原因是因为我在列表中使用了一个if理解,但是因为我能够看到结果,所以我知道这不是问题.我无法显示结果,不得不更改属性名称,因为这是我工作的公司.

这是追溯:

Elasticsearch.exceptions.RequestError: 
TransportError(400, 'action_request_validation_exception',
  'Validation Failed: 1: script or doc is missing...') 
Run Code Online (Sandbox Code Playgroud)

省略号表示它显示列表中每个元素失败的相同错误.

Obj*_*_88 12

基于文档很难说,但我发现了问题.如果要进行批量更新,则需要将源包装在字典中,密钥为"doc".这是正确的例子,希望这有帮助!

frequency_cleared = [
    {
        '_id': result['_id'], 
        "_type": "the-type", 
        "_index": "the-index", 
        "_source": {'doc': result['_source']}, 
        '_op_type': 'update'
    } 
    for result in search_results['hits']['hits']
]
Run Code Online (Sandbox Code Playgroud)

注意稍微改变是"_source"到{'doc':result ['_ source']}