# Any way to initialize a large set once in painless script for repeat use?

**URL:** <https://discuss.elastic.co/t/any-way-to-initialize-a-large-set-once-in-painless-script-for-repeat-use/146577>\
**Category:** Elasticsearch\
**Created:** [August 29, 2018, 8:03pm UTC](https://discuss.elastic.co/t/any-way-to-initialize-a-large-set-once-in-painless-script-for-repeat-use/146577 "2018-08-29T20:03:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbc](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@paulbc](https://discuss.elastic.co/u/paulbc)\
**Post date:** [August 29, 2018, 8:03pm UTC](https://discuss.elastic.co/t/any-way-to-initialize-a-large-set-once-in-painless-script-for-repeat-use/146577/1 "2018-08-29T20:03:36Z")

</div>

We want to implement filters using painless scripts that work by checking if a string is a member of some potentially large set (hundreds or thousands of members) determined ahead of time. E.g., suppose the set contains a statically determined whitelist or blacklist. Set membership should be fast once the set has been built, which is why we would like to preserve it between painless method calls.

I cannot find any way to do this. Of course, I can construct a new copy of the set (or a list) each time the painless script is called, but this amounts to doing linear search. Is there any kind of initialization that can be done once (essentially at class-loading time in the JVM) so that it does not need to be repeated for each call?

I realize that painless is somewhat limited and that I could look into writing custom Java scripts. I would like to avoid that for now. This seems like an application that other users would want. Am I missing something obvious?

---

<div class="post-metadata">

**Author:** ![Zhilong\_He](https://avatars.discourse-cdn.com/v4/letter/z/e19adc/32.png) [@Zhilong\_He](https://discuss.elastic.co/u/Zhilong_He)\
**Post date:** [August 30, 2018, 5:18am UTC](https://discuss.elastic.co/t/any-way-to-initialize-a-large-set-once-in-painless-script-for-repeat-use/146577/2 "2018-08-30T05:18:49Z")

</div>

maybe this resp can slove your problem：[flashtext](https://github.com/vi3k6i5/flashtext)

---

<div class="post-metadata">

**Author:** ![paulbc](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@paulbc](https://discuss.elastic.co/u/paulbc)\
**Post date:** [August 30, 2018, 7:31pm UTC](https://discuss.elastic.co/t/any-way-to-initialize-a-large-set-once-in-painless-script-for-repeat-use/146577/3 "2018-08-30T19:31:48Z")

</div>

I don't see how this would help, since it's written in python. Am I missing something? I am looking for a solution that runs as a painless script in the elasticsearch JVM.

---

<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:** [September 27, 2018, 7:32pm UTC](https://discuss.elastic.co/t/any-way-to-initialize-a-large-set-once-in-painless-script-for-repeat-use/146577/4 "2018-09-27T19:32:41Z")

</div>

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