# Groovy for scripting

**URL:** <https://discuss.elastic.co/t/groovy-for-scripting/19358>\
**Category:** Elasticsearch\
**Created:** [August 20, 2014, 9:11am UTC](https://discuss.elastic.co/t/groovy-for-scripting/19358 "2014-08-20T09:11:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_S\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_s_v/32/1317_2.png) [@Alex\_S\_V](https://discuss.elastic.co/u/Alex_S_V)\
**Post date:** [August 20, 2014, 9:11am UTC](https://discuss.elastic.co/t/groovy-for-scripting/19358/1 "2014-08-20T09:11:03Z")

</div>

I'm playing around with groovy scripting.

By checking groovy lang plugin source code I found next steps in code  
execution:

1. Code compilation into script class

2. Script initialization via static method newInstance()

3. Script execution via calling the code on each document with binding  
document parameters

Now assume I have class declaration in my script. Is it possible to execute  
class definition and class object initialization only once, and execute  
only a method from this object on each document?

Thanks

P.S. posting the same on SO

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/bb3a9ca6-79fd-4ac6-ac78-ce0c102b9505%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/bb3a9ca6-79fd-4ac6-ac78-ce0c102b9505%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Alex\_S\_V](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_s_v/32/1317_2.png) [@Alex\_S\_V](https://discuss.elastic.co/u/Alex_S_V)\
**Post date:** [August 26, 2014, 6:49pm UTC](https://discuss.elastic.co/t/groovy-for-scripting/19358/2 "2014-08-26T18:49:40Z")

</div>

providing self-update:

I found that I could create cross-request cache using next script (like a  
cross-request incrementer):

POST /test/\_search  
{  
query: {match\_all:{}},  
script\_fields: {  
a: {  
script: "import groovy.lang.Script;class A extends Script{static  
i=0;def run() {i++}}",  
lang: "groovy"  
}  
}  
}

In good view mode the script is:

import groovy.lang.Script

class A extends Script{  
static i=0

def run() {  
i++  
}  
}

Actually here _i_ variable is not thread-safe, but idea is clean - you need  
define a class, inherited from Script and implement abstract method run.  
Also this class is access on each node-thread.  
Now I'm looking for a solution to make a query-scope type counter (for  
one-node configuration). I think it's could be done by passing unique  
query\_id in parameters, but I'm afraid of making code non thread safe, or  
vice versa - thread safe, but with reduce performance.  
Researching more...

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fb402d2c-8820-4a1f-99e0-0453c0c82cf6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fb402d2c-8820-4a1f-99e0-0453c0c82cf6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:06am UTC](https://discuss.elastic.co/t/groovy-for-scripting/19358/3 "2017-07-06T01:06:15Z")

</div>

Closed on July 6, 2017, 1:06am UTC


