2

Looking for pointers to know how Groovy script can be invoked using java api.

test.groovy

def value = dynamicValue    
return value

Want to translate following query in Java:

GET /test-index/_search
{
   "query": {
      "match_all": {}
   },
   "script_fields": {
      "checkValue": {
         "script": "test",
         "params": {
            "dynamicValue": 7
         }
      }
   }
}
8
  • I must warn, make sure your ES-cluster cannot be called from outside (I had sudden shutdown, and Chinese Chicken roosting in my ES) both from HTTP as Transport Commented Aug 14, 2015 at 13:49
  • @Danielson : I am invoking ES to run when i have data to index using java-api & after it is done I am searching for it & in one of the case I need to filter data based on some condition that is written in groovy placed in ES directory. In this case, how ES-cluster can be called from outside ? I didn't get properly,can you please explain? Commented Aug 17, 2015 at 5:05
  • 1
    First of, if your version is > v1.4.3 and you haven't changed script.groovy.sandbox.enabled: false to script.groovy.sandbox.enabled: true, then ignore me! Otherwise, you need to check whether you can access your cluster by your_external_ip_address:9200 (you shouldn't get a response). Try to connect as a Node to your cluster from a far-away computer, you must not be able to access, try like Client CLIENT = new TransportClient(ImmutableSettings.settingsBuilder().put("cluster.name", "your_name").build()).addTransportAddress(new InetSocketTransportAddress("external_ip", 9300));. Commented Aug 17, 2015 at 6:45
  • 1
    Btw, this was what I was talking about elastic.co/guide/en/elasticsearch/reference/current/… Commented Aug 17, 2015 at 6:47
  • 1
    according to the elastic.co link: If you are running a vulnerable version of Elasticsearch, you should either upgrade to at least v1.3.8 or v1.4.3, or disable dynamic Groovy scripts by adding this setting to the config/elasticsearch.yml file in all nodes in the cluster: script.groovy.sandbox.enabled: false as you said. So according to ES, you're fine... Commented Aug 17, 2015 at 9:21

1 Answer 1

2

You can do it like this:

Map<String, Object> params = ImmutableMap.of("dynamicValue", 7);
SearchResponse response = client().prepareSearch("test-index")
        .setQuery(matchAllQuery())
        .addScriptField("checkValue", new Script("test", ScriptType.FILE, "groovy", params))
        .execute().actionGet();

You need to store your test.groovy file in the config/scripts folder on each data node and also make sure scripting is enabled in config/elasticsearch.yml with

script.inline: on
script.file: on
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Val for your quick response :) But i am wondering can it also be done using FilterBuilders.scriptFilter() which returns ScriptFilterBuilder ? Basically i want to filter my data based on some condition which will be passed as params.
Yes, you can do it as shown here, however, I'm not certain you can use scripts stored in files, only inlined ones.
The hyperlink is broken
@AjayTakur this is very old stuff, I believe you described your issues in this thread

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.