【问题标题】:Bulk update with Python's elasticsearch client使用 Python 的 elasticsearch 客户端进行批量更新
【发布时间】:2016-05-12 23:11:03
【问题描述】:

我正在尝试根据文档属性的状态更改进行批量更新。 Create 工作正常,但 bulk 吓坏了。我收到“缺少脚本或文档”的错误,但一切看起来都很好。

这是我尝试批量更新的方式:

frequency_cleared = [
    {
        "_id": result['_id'], 
        "_type": "the-type", 
        "_index": "the-index", 
        "_source": result['_source'],
        "_op_type": 'update'
    } 
    for result in search_results['hits']['hits']
]

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

这是回溯:

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

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

【问题讨论】:

    标签: python elasticsearch elasticsearch-py


    【解决方案1】:

    根据文档很难判断,但我发现了问题所在。如果要进行批量更新,则需要将源代码包装在字典中,键为“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']
    ]
    

    注意细微的变化是“_source”到{'doc': result['_source']}

    【讨论】:

    • 这很有用 - 他们的 python docs 似乎不准确。在那里,“doc”没有包装在“_source”对象中。
    猜你喜欢
    • 2021-12-24
    • 2016-05-02
    • 2017-09-06
    • 1970-01-01
    • 2018-10-15
    • 1970-01-01
    • 2018-07-23
    • 2012-12-09
    • 1970-01-01
    相关资源
    最近更新 更多