# Using \_update\_by\_query and painless script with hashmap to update based on value

**URL:** <https://discuss.elastic.co/t/using-update-by-query-and-painless-script-with-hashmap-to-update-based-on-value/177954>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [April 23, 2019, 6:36am UTC](https://discuss.elastic.co/t/using-update-by-query-and-painless-script-with-hashmap-to-update-based-on-value/177954 "2019-04-23T06:36:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pa2000cer](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@pa2000cer](https://discuss.elastic.co/u/pa2000cer)\
**Post date:** [April 23, 2019, 6:36am UTC](https://discuss.elastic.co/t/using-update-by-query-and-painless-script-with-hashmap-to-update-based-on-value/177954/1 "2019-04-23T06:36:26Z")

</div>

I have a huge list of keys and values such as this

[{'appid':12345,'lob': 'Finance'},  
{'appid':67891,'lob': 'Banking'},  
{'appid':11111,'lob': 'Finance'},  
{'appid':22222,'lob': 'Fast Food'}]

I want to create a script and the update\_by\_query to do this

Search all documents where the field AppId (document field value) = appid (from hashmap) and then update the LOB field in the document based on that value.

I'm having a hard time figuring out where to start but I think this is possible.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 23, 2019, 4:20pm UTC](https://discuss.elastic.co/t/using-update-by-query-and-painless-script-with-hashmap-to-update-based-on-value/177954/2 "2019-04-23T16:20:13Z")

</div>

Hey,

in order to properly find the correct documents, you need to have that array of data indexed as a `nested` type. Otherwise you would need search all documents and check in your script if there is a single element, that has appId=Foo and lob=Bar.

Can you explain where you are having problems? Is it coming up with the painless script? If so, which part of it exactly? the part of checking if you need to do a replacing or the replacing itself?

--Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 21, 2019, 4:20pm UTC](https://discuss.elastic.co/t/using-update-by-query-and-painless-script-with-hashmap-to-update-based-on-value/177954/3 "2019-05-21T16:20:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
