# Creating and using custom functions in painless

**URL:** <https://discuss.elastic.co/t/creating-and-using-custom-functions-in-painless/351037>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [January 14, 2024, 7:17pm UTC](https://discuss.elastic.co/t/creating-and-using-custom-functions-in-painless/351037 "2024-01-14T19:17:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dat\_boi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dat\_boi](https://discuss.elastic.co/u/dat_boi)\
**Post date:** [January 14, 2024, 7:17pm UTC](https://discuss.elastic.co/t/creating-and-using-custom-functions-in-painless/351037/1 "2024-01-14T19:17:35Z")

</div>

so here is the thing , i have this long script that define variables of type  
`String[] words_var1 = new String[] {'word1','word1','word1'}`  
then i have this function that tries to assign the right word to the right doc based on an id like so

```auto
 
def addwords(id, words) {
    if (ctx._source.fiiled1.contains(id)) {

        if (ctx._source.fiiled2 == null) {
            ctx._source.fiiled2 = [];
        }
        ctx._source.fiiled2.addAll(Arrays.asList(words));
    }
};

 addwords(1, words_var1);

```

i'm not even sure what the error that i get mean and i can't find it cause the script is too long ,

```auto
 "script_stack": [
          """... def addwords(id, words) {
    if ...""",
          " ^---- HERE"
        ],

```

```auto
 "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "unexpected token ['('] was expecting one of [{<EOF>, ';'}]."
    }

```

so i never wrote painless before and i really can't find good examples about so don't go hard on me as i suspect that not the way to even define a function

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [January 14, 2024, 8:18pm UTC](https://discuss.elastic.co/t/creating-and-using-custom-functions-in-painless/351037/2 "2024-01-14T20:18:00Z")

</div>

I am not good at painless either but have done a little.

First question: are you trying to use in an ingest pipeline ..what is the context?

1. Definitely look at the docs [here](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-debugging.html)

The example in detail here that shows [the proper way to define a function](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-functions.html) you are missing the `def`s

```auto
def addwords(def id, def words) {

```

3 you can use the Dev Tools - Painless debugger to work on syntax

```auto
def addwords(def id, def words) {
    String[] fiiled2 = new String[] {'foo','bar','word1'};
    if (id.equals(1)) {
      return fiiled2[id];
    }
    return words[id];
}

String[] words_var1 = new String[] {'word1','word1','word1'};
addwords(1, words_var1);

```

So you can use the debugger you will just need to substitute the context variables with some locals to debug ... then switch back to `_ctx.`

 ![Screenshot 2024-01-14 at 12.16.22 PM](https://us1.discourse-cdn.com/elastic/original/3X/6/0/60c4aa044e07d25c3699c6f73408db6e2b8897e3.png)

---

<div class="post-metadata">

**Author:** ![dat\_boi](https://avatars.discourse-cdn.com/v4/letter/d/3ec8ea/32.png) [@dat\_boi](https://discuss.elastic.co/u/dat_boi)\
**Post date:** [January 15, 2024, 12:01am UTC](https://discuss.elastic.co/t/creating-and-using-custom-functions-in-painless/351037/3 "2024-01-15T00:01:43Z")

</div>

thanks for the help @stephenb , that solved the syntax issues plus i had to move variables declarations to after declaring the function.

---

<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:** [February 12, 2024, 12:02am UTC](https://discuss.elastic.co/t/creating-and-using-custom-functions-in-painless/351037/4 "2024-02-12T00:02:32Z")

</div>

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