# Most efficient way to do a lookup (\<100 items) in a painless script

**URL:** <https://discuss.elastic.co/t/most-efficient-way-to-do-a-lookup-100-items-in-a-painless-script/159718>\
**Category:** Elasticsearch\
**Created:** [December 6, 2018, 11:23am UTC](https://discuss.elastic.co/t/most-efficient-way-to-do-a-lookup-100-items-in-a-painless-script/159718 "2018-12-06T11:23:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamalarm](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Jamalarm](https://discuss.elastic.co/u/Jamalarm)\
**Post date:** [December 6, 2018, 11:23am UTC](https://discuss.elastic.co/t/most-efficient-way-to-do-a-lookup-100-items-in-a-painless-script/159718/1 "2018-12-06T11:23:40Z")

</div>

Hi,

I need to do some aggregations on a scripted term. The term will be calculated from a keyword field stored in my index.

The script will need to:

1. Grab the first substring before a whitespace character
2. Check it against a list/map of values and see if it matches any of them
3. If it matches, rewrite the term to a new mapped value, otherwise keep the original

I'm wondering what the most efficient way to handle step 2 would be in a painless script. My initial thought was using a `Map<String,String>`, but then I thought this might mean the map is discarded and created for every single document that gets passed through the script. Would it be more efficient to do a really big if-else stack? As much as I dislike this idea, performance will be important here, and this has to be done via script because the mappings might change over time.

Would really appreciate any insight on this - thanks for your help!

Tom

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [December 6, 2018, 11:50am UTC](https://discuss.elastic.co/t/most-efficient-way-to-do-a-lookup-100-items-in-a-painless-script/159718/2 "2018-12-06T11:50:01Z")

</div>

If performance is important, the more processing you can do before indexing the document, the bettet.

---

<div class="post-metadata">

**Author:** ![Jamalarm](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Jamalarm](https://discuss.elastic.co/u/Jamalarm)\
**Post date:** [December 6, 2018, 1:00pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-do-a-lookup-100-items-in-a-painless-script/159718/3 "2018-12-06T13:00:38Z")

</div>

Oh, for sure, but given I can't pre-process here as the list/map will change (and the output must change retroactively), this has to happen in realtime.

---

<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:** [January 3, 2019, 1:00pm UTC](https://discuss.elastic.co/t/most-efficient-way-to-do-a-lookup-100-items-in-a-painless-script/159718/4 "2019-01-03T13:00:46Z")

</div>

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