Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastisearch update by query

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).

like image 987
AhmyOhlin Avatar asked Feb 27 '17 15:02

AhmyOhlin


1 Answers

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.

like image 132
christinabo Avatar answered Oct 04 '22 05:10

christinabo