我在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
请注意,在撰写本文时,更新一次只能在一个文档上执行。 将来,Elasticsearch可能会在给定查询条件的情况下更新多个文档(如SQL UPDATE-WHERE语句)。
I am using this code in python for updating my docs in elasticsearch. It's working fine but it's difficult to use it for a millions docs because I have to initialise the id value everytime to update every document.
from elasticsearch import Elasticsearch, exceptions elasticsearch = Elasticsearch() elasticsearch.update(index='testindex', doc_type='AAA', id='AVpwMmhnpIpyZkmdMQkT', body={ 'doc':{'Device': 'updated'} } )I read in the Elasticsearch documentation that it's not yet included but: 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更新值。
我希望它也适合你,可能会对你想要使用的查询进行一些调整。
Using the update_by_query (not the update) and the script, you should be able to update the documents that match your query.
q = { "script": { "inline": "ctx._source.Device='Test'", "lang": "painless" }, "query": { "match": { "Device": "Boiler" } } } es.update_by_query(body=q, doc_type='AAA', index='testindex')The above worked for me. The q finds the documents that match your query and the script updates the value using the _source of each document.
I hope it works for you too, possibly with some adjustment on the query you want to use.
更多推荐
发布评论