# Import java classes into painless script

**URL:** <https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699>\
**Category:** Elasticsearch\
**Created:** [May 5, 2018, 6:06am UTC](https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699 "2018-05-05T06:06:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_Med\_Student](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@A\_Med\_Student](https://discuss.elastic.co/u/A_Med_Student)\
**Post date:** [May 5, 2018, 6:06am UTC](https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699/1 "2018-05-05T06:06:38Z")

</div>

Hi,  
I am migrating from an old version of elasticsearch (1.4.4). I had a groovy script, that was importing classes from the java classpath. With my earlier script I was executing updates, conditionally.

With the new version (6.2.3), groovy is no longer supported and I have no idea how I can achieve the same functionality with painless.

I do not know whether I need to implement a ScriptEngine, and if yes, where can I find some examples for a ScriptEngine that will do updates? I have searched on google and also these forums, but have found nothing beyond the boilerplate code for a SearchScript Engine.

Here is some example code from my earlier script:

```
import mypackage.class.name 
import mypackage.otherclass.name

OtherClass ex = new OtherClass();
ctx._source.text = "something";
ctx._source.ex1 = ex;

```

If anyone can guide me on how to implement updates using a ScriptEngine , I would be really obliged. Or alternatively, how do I import my custom java classes into a Painless Script?

Many Thanks,  
B.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 11, 2018, 4:26pm UTC](https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699/2 "2018-05-11T16:26:48Z")

</div>

As of 6.2, painless can now have it's whitelist extended by plugins. Unfortunately this is not well documented yet, but we do have an [example plugin](https://github.com/elastic/elasticsearch/tree/6.2/plugins/examples/painless-whitelist).

The basic idea is to create a plugin that "extends" painless, and tells painless about additional whitelist elements through SPI. The whitelist is per script context, so you will need to add these elements for any types of scripts you use. There is no central list of script contexts, but it looks like in this case you are using an update script? So that would be the `update` context. In your script, you can then use the classes, no importing necessary.

---

<div class="post-metadata">

**Author:** ![A\_Med\_Student](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@A\_Med\_Student](https://discuss.elastic.co/u/A_Med_Student)\
**Post date:** [May 22, 2018, 10:32am UTC](https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699/3 "2018-05-22T10:32:42Z")

</div>

Hi,  
Thank you for your reply.  
Unfortunately, I am not able to gather enough information to know how to do this. Can you please give me a simple example script?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 22, 2018, 5:20pm UTC](https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699/4 "2018-05-22T17:20:47Z")

</div>

Please see the example plugin I linked in my last comment.

---

<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:** [June 19, 2018, 5:20pm UTC](https://discuss.elastic.co/t/import-java-classes-into-painless-script/130699/5 "2018-06-19T17:20:47Z")

</div>

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