Import java classes into painless script


(A Med Student) #1

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.


(Ryan Ernst) #2

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.

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.


(A Med Student) #3

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?


(Ryan Ernst) #4

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


(system) #5

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