# How does painless work?

**URL:** <https://discuss.elastic.co/t/how-does-painless-work/155077>\
**Category:** Elasticsearch\
**Created:** [November 1, 2018, 8:39pm UTC](https://discuss.elastic.co/t/how-does-painless-work/155077 "2018-11-01T20:39:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bps](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@bps](https://discuss.elastic.co/u/bps)\
**Post date:** [November 1, 2018, 8:39pm UTC](https://discuss.elastic.co/t/how-does-painless-work/155077/1 "2018-11-01T20:39:40Z")

</div>

I have been using painless saved on my ES cluster for a while now on a project and little is really documented as far as how it works "under the covers" of elastic search -- at least I can't find much. Can any of the experts tell me:

1. How is a Painless script executed on an ES cluster once a query is received? That is, how is it executed differently than using a query?
2. Does sharding have a similar impact to performance for Painless scripts as ES queries?
3. Are Painless scripts executed in a multithreaded fashion?
4. What may be some best practices when using Painless?
5. Are there some field types stored on an ES cluster that work better with painless (e.g. keyword vs. integer)?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [November 9, 2018, 8:09pm UTC](https://discuss.elastic.co/t/how-does-painless-work/155077/2 "2018-11-09T20:09:22Z")

</div>

> [@bps](#):
>
> How is a Painless script executed on an ES cluster once a query is received? That is, how is it executed differently than using a query?

there are many different places we use scripts. For script query we execute the given script for every document that matches the query. Not sure if that answers your question.

> [@bps](#):
>
> Does sharding have a similar impact to performance for Painless scripts as ES queries?

sharding is our way for parallelism. 1 search request corresponds to a thread on a shard (simplified). The more shards the more parallelism. Yet, the script perf as for a single doc doesn't change.

> [@bps](#):
>
> Are Painless scripts executed in a multithreaded fashion?

per shared sequentially for a single request, see above.

> [@bps](#):
>
> What may be some best practices when using Painless?

not sure how to answer this.

> [@bps](#):
>
> Are there some field types stored on an ES cluster that work better with painless (e.g. keyword vs. integer)?

I think numbers are in general preferable over strings.

---

<div class="post-metadata">

**Author:** ![bps](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@bps](https://discuss.elastic.co/u/bps)\
**Post date:** [November 18, 2018, 3:28pm UTC](https://discuss.elastic.co/t/how-does-painless-work/155077/3 "2018-11-18T15:28:43Z")

</div>

Thanks for your responses, Simon. This does help some. Just to clarify my understanding, you mentioned: a painless script will execute once for each document matched in the query. So, if for example, I have an ES index containing 20 million documents and a query that returns 2 million of them, the script will execute 2 million times, one for each matching document?

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [November 21, 2018, 6:42am UTC](https://discuss.elastic.co/t/how-does-painless-work/155077/4 "2018-11-21T06:42:51Z")

</div>

> [@bps](#):
>
> painless script will execute once for each document matched in the query. So, if for example, I have an ES index containing 20 million documents and a query that returns 2 million of them, the script will execute 2 million times, one for each matching document?

well if you have a [script\_query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-query.html#query-dsl-script-query) for instance that you use to match the query we have to execute that script for every document that can potentially match the document. If you have 20 million matches, it will have been executed 20million times at least. But this is a lower bound, it depends on the rest of your query how often we have to check the result of the scrip to make a decision.

does this make sense?

---

<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:** [December 19, 2018, 6:43am UTC](https://discuss.elastic.co/t/how-does-painless-work/155077/5 "2018-12-19T06:43:06Z")

</div>

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