How to update a document using elasticsearch-py?

48,996

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 }})
Share:
48,996
Dan Hook
Author by

Dan Hook

Updated on July 09, 2022

Comments

  • Dan Hook
    Dan Hook almost 2 years

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

  • Admin
    Admin over 8 years
    and what if use this with update_by_query api :) There should be no id parameter I guess.. Or ?
  • Val
    Val over 8 years
    No that's different. update-by-query is an external plugin, not something that is supported by the elasticsearch-py library.
  • Admin
    Admin over 8 years
    Is it possible use update_by_query in python with http request?
  • David Kong
    David Kong over 4 years
    update by query for elasticsearch-py documentation is here elasticsearch-py.readthedocs.io/en/master/…
  • user8291021
    user8291021 about 4 years
    If you are using elasticsearch 7x - remember to remove doc_type