57

Does anyone have an example for how to use update? It's documented here, but the documentation is unclear and doesn't include a working example. I've tried the following:

coll = Elasticsearch()
coll.update(index='stories-test',doc_type='news',id=hit.meta.id,
                body={"stanford": 1, "parsed_sents": parsed })

and I get

elasticsearch.exceptions.RequestError: 
TransportError(400, u'ActionRequestValidationException[Validation Failed: 1: script or doc is missing;]')

I would like to update using a partial doc, but the update method doesn't take any argument named 'doc' or 'document'.

W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
Dan Hook
  • 6,769
  • 7
  • 35
  • 52

1 Answers1

97

You're almost there, you just need to enclose your body inside a "doc" field. The correct way of doing a partial update with elasticsearch-py goes like this:

coll = Elasticsearch()
coll.update(index='stories-test',doc_type='news',id=hit.meta.id,
                body={"doc": {"stanford": 1, "parsed_sents": parsed }})
Val
  • 207,596
  • 13
  • 358
  • 360
  • and what if use this with update_by_query api :) There should be no id parameter I guess.. Or ? –  Oct 05 '15 at 10:33
  • 2
    No that's different. update-by-query is an external plugin, not something that is supported by the elasticsearch-py library. – Val Oct 05 '15 at 10:48
  • 1
    Is it possible use update_by_query in python with http request? –  Oct 05 '15 at 13:54
  • update by query for elasticsearch-py documentation is here https://elasticsearch-py.readthedocs.io/en/master/api.html#elasticsearch.Elasticsearch.update_by_query – David Kong Dec 25 '19 at 07:43
  • 8
    If you are using elasticsearch 7x - remember to remove doc_type – user8291021 May 15 '20 at 20:55
  • Can you please tell on how to update the list?? Like what argument to use @Val – BrownBatman Jan 26 '23 at 15:18
  • @BrownBatman what list? please don't hesitate to create a new thread with your specific issue, as this thread is closed – Val Jan 26 '23 at 15:28