# Elasticsearch scripting: Invoke custom script from plugin

**URL:** <https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835>\
**Category:** Elasticsearch\
**Created:** [March 30, 2016, 5:54pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835 "2016-03-30T17:54:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thanh\_Hai\_Ngo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanh_hai_ngo/32/8769_2.png) [@Thanh\_Hai\_Ngo](https://discuss.elastic.co/u/Thanh_Hai_Ngo)\
**Post date:** [March 30, 2016, 5:54pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/1 "2016-03-30T17:54:47Z")

</div>

If we defined native script in plugin: com.test.nativescript.TestScript, can we invoke that package inside another groovy script and create new instance ?

Ex:  
inside config/scripts/test.groovy:

```
import com.test.nativescript.TestScript;

def testScript = new TestScript();

```

Result: "startup failed:\r\nc78a82bc96ca7751e83e256b4ee0c322cda13bea: 1: unable to resolve com.test.nativescript.TestScript"

---

<div class="post-metadata">

**Author:** ![Thanh\_Hai\_Ngo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanh_hai_ngo/32/8769_2.png) [@Thanh\_Hai\_Ngo](https://discuss.elastic.co/u/Thanh_Hai_Ngo)\
**Post date:** [March 31, 2016, 12:56pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/2 "2016-03-31T12:56:48Z")

</div>

Someone help me please, I really need it.

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [April 1, 2016, 11:25am UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/3 "2016-04-01T11:25:05Z")

</div>

The error you get seems to say that you cannot do this. May I ask you what you intend to do? You already wrote a script plugin that you can use, why would another script need to use it?

---

<div class="post-metadata">

**Author:** ![Thanh\_Hai\_Ngo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanh_hai_ngo/32/8769_2.png) [@Thanh\_Hai\_Ngo](https://discuss.elastic.co/u/Thanh_Hai_Ngo)\
**Post date:** [April 1, 2016, 12:57pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/4 "2016-04-01T12:57:29Z")

</div>

@javanna: this is common script which I use cross multiple scripts.  
Look like it's impossible due to security check.

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [April 1, 2016, 12:59pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/5 "2016-04-01T12:59:39Z")

</div>

That's what I thought, but again I am not sure why you would need that, maybe you can refactor your scripts into a single one, or provide a better explanation of why so we can help you.

Cheers  
Luca

---

<div class="post-metadata">

**Author:** ![Thanh\_Hai\_Ngo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanh_hai_ngo/32/8769_2.png) [@Thanh\_Hai\_Ngo](https://discuss.elastic.co/u/Thanh_Hai_Ngo)\
**Post date:** [April 1, 2016, 2:36pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/6 "2016-04-01T14:36:51Z")

</div>

@javanna: Thanks so much for your help.  
My scenario:  
Previously with ES 1.5, i have native script to calculate total amount (logic is very complex). then I have multiple scripts run with facet plugin using map-reduce approach. Inside those scripts, get total script to calculate amount, then use it for some logic:

```
compiledScript = scriptService.compile('native','amount', null);
executableScript = scriptService.executable(compiledScript, param);
amount = executableScript.run(doc())

```

Now I want to upgrade to ES 2.0 without facet support, so I try to use aggregation. But due to security problem, now no way to access native to do that.

I tried to switch solution, build other native which used to call groovy map-reduce scripts but still stuck: [https://github.com/elastic/elasticsearch/issues/17461](https://github.com/elastic/elasticsearch/issues/17461)  
Even with native, I cannot access ScriptService, only GroovyScriptEngineService and couldn't invoke File Scripts.

Did I do anything wrong or we cannot do it ?  
Please advice.  
Thanks.

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [April 1, 2016, 2:48pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/7 "2016-04-01T14:48:34Z")

</div>

At first glance I would say that you are doing too much from within a script and most of this logic should be done outside of elasticsearch instead. When scripts become too complex they slow searches down and become very complex to maintain. And the fact that you have multiple scripts that depend on each other makes it even worse I think. We did work a lot on security from 2.0 on, so I am not surprised that you cannot do anymore what you used to be able to do in 1.x from a script.

It would probably be a good idea to try and take out of scripts some of your complex logic and do this processing work outside of elasticsearch.

---

<div class="post-metadata">

**Author:** ![Thanh\_Hai\_Ngo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thanh_hai_ngo/32/8769_2.png) [@Thanh\_Hai\_Ngo](https://discuss.elastic.co/u/Thanh_Hai_Ngo)\
**Post date:** [April 1, 2016, 2:59pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/8 "2016-04-01T14:59:25Z")

</div>

Thank for your help. I'm clear about that problem.

---

<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:** [July 5, 2017, 11:03pm UTC](https://discuss.elastic.co/t/elasticsearch-scripting-invoke-custom-script-from-plugin/45835/9 "2017-07-05T23:03:13Z")

</div>


