# How to externalise painless scripts and call it

**URL:** <https://discuss.elastic.co/t/how-to-externalise-painless-scripts-and-call-it/245497>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 18, 2020, 9:37pm UTC](https://discuss.elastic.co/t/how-to-externalise-painless-scripts-and-call-it/245497 "2020-08-18T21:37:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [August 18, 2020, 9:37pm UTC](https://discuss.elastic.co/t/how-to-externalise-painless-scripts-and-call-it/245497/1 "2020-08-18T21:37:40Z")

</div>

hi  
I have looked into the few examples, of painless script  
[https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-execute-api.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-execute-api.html)  
A current example shown is

```auto
POST /_scripts/painless/_execute
{
  "script": {
    "source": "params.count / params.total",
    "params": {
      "count": 100.0,
      "total": 1000.0
    }
  }
}

```

But all the examples show "inline" script of 1 or 2 lines, but we got a complex logic and wanted to externalise the painless script, so that I can pass parameters into the script and get return value (a typical function call concept)

the one I'm looking forward is something like

```auto
POST /_scripts/painless/_execute
{
  "script": {
    "name": "my_external_script(count,total)",
    "params": {
      "count": 100.0,
      "total": 1000.0
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 18, 2020, 10:35pm UTC](https://discuss.elastic.co/t/how-to-externalise-painless-scripts-and-call-it/245497/2 "2020-08-18T22:35:29Z")

</div>

Scripts can be stored in Elasticsearch using the [Scripts API](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#modules-scripting-stored-scripts), giving the script an id that can then be later used to reference it. For example

to store the script

```json
POST _scripts/calculate-avg
{
  "script": {
    "lang": "painless",
    "source": "params.count / params.total"
  }
}

```

Then, to use

```json
POST /_scripts/painless/_execute
{
  "script": {
    "id": "calculate-avg",
    "params": {
      "count": 100.0,
      "total": 1000.0
    }
  }
}

```

You may also be interested in checking out the [Painless lab in Kibana](https://www.elastic.co/guide/en/kibana/current/painlesslab.html), to help in writing more complex Painless scripts

---

<div class="post-metadata">

**Author:** ![kelk](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kelk](https://discuss.elastic.co/u/kelk)\
**Post date:** [August 19, 2020, 10:31am UTC](https://discuss.elastic.co/t/how-to-externalise-painless-scripts-and-call-it/245497/3 "2020-08-19T10:31:30Z")

</div>

perfect. thank mate

---

<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 16, 2020, 10:31am UTC](https://discuss.elastic.co/t/how-to-externalise-painless-scripts-and-call-it/245497/4 "2020-09-16T10:31:32Z")

</div>

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