Skip to content

How to append to an Array in Elasticsearch using elasticsearch-py

If you are using the Official ElasticSearch Python library (Docs), and you want to create an index:

doc = { "something": "123a", "somethingelse": "456b", "timestamp": datetime.now(), "history": [] } es.index(index="someindex", doc_type="somedoctype", id="someid", body=doc)
Code language: Python (python)

You can append items to the history each time, instead of overriding them, like this:

es.update(index="someindex", doc_type="somedoctype", id="someid", body={ "script" : { "source": "ctx._source.history.addAll(params.history)", "lang": "painless", "params" : { "history" : ["item1","item2"] } } })
Code language: Python (python)

This uses scripted updates in Elasticsearch, for appending to an array.

See also  Flash Tweening
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x