gpt4 book ai didi

python - Elasticsearch 通过查询更新

转载 作者:太空狗 更新时间:2023-10-29 22:12:32 24 4
gpt4 key购买 nike

我在 python 中使用这段代码来更新我在 elasticsearch 中的文档。它工作正常,但很难将它用于数百万文档,因为我每次都必须初始化 id 值以更新每个文档。

from elasticsearch import Elasticsearch, exceptions

elasticsearch = Elasticsearch()

elasticsearch.update(index='testindex', doc_type='AAA', id='AVpwMmhnpIpyZkmdMQkT',
body={
'doc':{'Device': 'updated'}
}
)

我在 Elasticsearch 文档中读到它尚未包含在内,但是: https://www.elastic.co/guide/en/elasticsearch/reference/current/_updating_documents.html

Note that as of this writing, updates can only be performed on a single document at a time. In the future, Elasticsearch might provide the ability to update multiple documents given a query condition (like an SQL UPDATE-WHERE statement).

最佳答案

使用 update_by_query(不是 update)和 script,您应该能够更新与您的查询匹配的文档。

 q = {
"script": {
"inline": "ctx._source.Device='Test'",
"lang": "painless"
},
"query": {
"match": {
"Device": "Boiler"
}
}
}

es.update_by_query(body=q, doc_type='AAA', index='testindex')

以上对我有用。 q 查找与您的查询匹配的文档,脚本使用每个文档的 _source 更新值。

我希望它也适用于您,可能需要对您要使用的查询进行一些调整。

关于python - Elasticsearch 通过查询更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com