Elastisearch通过查询更新(Elastisearch update by query)

编程入门 行业动态 更新时间:2024-10-09 23:13:51
Elastisearch通过查询更新(Elastisearch update by query)

我在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.

更多推荐

本文发布于:2023-07-27 07:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287112.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Elastisearch   update   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!