【问题标题】:Bulk Partial Upsert in Elasticseach with python使用 python 在 Elasticsearch 中批量部分 Upsert
【发布时间】:2016-03-30 07:07:33
【问题描述】:

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

使用批量助手,我尝试了很多变体,但它们都消除了现有值以支持新值。

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

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

也不行。

【问题讨论】:

    标签: python elasticsearch upsert


    【解决方案1】:

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

    【讨论】:

    • 不知道_id怎么办?
    • 很久没做弹性搜索了。但是我觉得, _id 在 upsert 中是必需的,否则您无法确定文档是否存在。如果您不知道,则必须进行另一个查询以获取您要更新的所有 id。
    【解决方案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')
    

    【讨论】:

    • doc_as_upsert 没有区别。此外,我希望操作为index,如果您将其排除在外,这是默认设置。最后,我使用的是与您发布的语法不同的批量 API。
    • 我相信index 操作总是会覆盖现有值(如果存在)。
    • 不确定如何使用index。我的答案使用update,它的行为会像你预期的那样
    • 对不起,我的语法错误,我认为上面的工作。
    猜你喜欢
    • 2018-05-25
    • 1970-01-01
    • 2014-07-17
    • 2021-12-24
    • 2014-11-27
    • 2019-02-16
    • 2022-07-04
    • 2016-01-18
    • 2012-12-09
    相关资源
    最近更新 更多