Groovy for scripting


(Alex S.V.) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/bb3a9ca6-79fd-4ac6-ac78-ce0c102b9505%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Alex S.V.) #2

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/fb402d2c-8820-4a1f-99e0-0453c0c82cf6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #3