gignosko
gignosko

Reputation: 987

Elasticsearch: update multiple docs with python script?

I have a scenario where we have an array field on documents and we're trying to update a key/value in that array on each document, so for instance a doc would look like:

_source:{
   "type": 1,
   "items": [
      {"item1": "value1"},
      {"item2": "value2"}
   ]
}

We're trying to efficiently update "value1" for instance on every doc of "type": 1. We'd like to avoid conflicts and we're hoping we can do this all by using a scrip, preferably in python, but I can't find any examples of how to update fields in python, let alone across multiple documents.

So, is it possible to do this with a script and if so, does anyone have a good example?

Thanks

Upvotes: 1

Views: 1196

Answers (1)

JWinkler05
JWinkler05

Reputation: 26

I know this is a little late, but I came across this in a search so I figured I would answer for anyone else who comes by. You can definitely do this utilizing the elasticsearch python library.

You can find all of the info and examples you need via the Elasticsearch RTD.

More specifically, I would look into the "ingest" operations as you can update specific pieces of documents within an index using elasticsearch.

So your script should do a few things:

  1. Gather list of documents using the "search" operation
  2. Depending on size, or if you want to thread, you can store in a queue
  3. Loop through list of docs / pop doc off queue
  4. Create updated field list
  5. Call "update" operations EX: self.elasticsearch.update(index=MY_INDEX, doc_type=1, id=123456, body={"item1": "updatedvalue1"})

Upvotes: 1

Related Questions