Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a document using elasticsearch-py?

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

like image 911
Dan Hook Avatar asked Jun 02 '15 13:06

Dan Hook


People also ask

Can you update a document in Elasticsearch?

The script can update, delete, or skip modifying the document. The update API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API.

Is Elasticsearch good for updates?

Elasticsearch allows us to do partial updates, but internally these are “get_then_update” operations, where the whole document is fetched, the changes are applied and then the document is indexed again. Even without disk hits one can imagine the potential performance implications if this is your main use case.

What is Doc_type Elasticsearch?

doc_type – The type of the document; deprecated and optional starting with 7.0. _source – True or false to return the _source field or not, or a list of fields to return.


1 Answers

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 }}) 
like image 163
Val Avatar answered Sep 29 '22 01:09

Val