Reputation: 549
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).
Upvotes: 14
Views: 25357
Reputation: 1130
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.
Upvotes: 28