12

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

AhmyOhlin
  • 519
  • 4
  • 8
  • 18
  • I am pretty sure that `update_by_query` that gets as a parameter the q should work. Just check this for python http://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.update_by_query – christinabo Feb 27 '17 at 15:45
  • Hello AhmyOhlin and welcome to the site. I edited your question to match the format of the other questions on this site, feel free to edit it again. – MackM Feb 27 '17 at 17:53
  • @christinabo i want to change the value of Device from 'Boiler' to 'Test'. i use the parameter q ='Device:"Boiler"' to update all docs with value 'boiler' but i got the following error: TypeError: update() got an unexpected keyword argument 'q' this is my code `elasticsearch.update(index='testindex', doc_type='AAA', q ='Device:"Boiler"', body={ 'doc':{'Device': 'TESTs'} }` – AhmyOhlin Feb 28 '17 at 08:24
  • Possible duplicate of [Elasticsearch Update by Query](http://stackoverflow.com/questions/40265578/elasticsearch-update-by-query) – Blairg23 Feb 28 '17 at 08:29
  • @Blairg23: my Problem is to search all docs contains `Device:"Boiler"'` then update it my question is: how can i use the q parameter in update help op python says: q – Query in the Lucene query string syntax i don't understand how i use it because there is no example. – AhmyOhlin Feb 28 '17 at 08:55

1 Answers1

26

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.

christinabo
  • 1,110
  • 1
  • 13
  • 18