且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用elasticsearch-py更新文档?

更新时间:2023-02-14 08:21:25

你几乎在那里,只需要将你的身体放在doc字段中。使用elasticsearch-py进行部分更新的正确方法如下所示:

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


Does anyone have an example for how to use update? It's documented here, but the documentation is unclear and doesn't included 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'.

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