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
         }
      }
   }
}
Tushar Khanna
  • 428
  • 6
  • 22
  • 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 – Danielson Aug 14 '15 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? – Tushar Khanna Aug 17 '15 at 05: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));`. – Danielson Aug 17 '15 at 06:45
  • 1
    Btw, this was what I was talking about https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html – Danielson Aug 17 '15 at 06:47
  • @Danielson : I am using `v1.3.2` and this is in my configuration file : `script.default_lang: groovy script.disable_dynamic: true script.groovy.sandbox.enabled: false` So, I guess i should not worry,right? – Tushar Khanna Aug 17 '15 at 08:20
  • 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... – Danielson Aug 17 '15 at 09:21
  • @Danielson : Can you answer this question ? http://stackoverflow.com/questions/32036346/update-index-update-a-search-analyzer – Tushar Khanna Aug 17 '15 at 11:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87157/discussion-between-tushar-khanna-and-danielson). – Tushar Khanna Aug 17 '15 at 11:03

1 Answers1

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
Danielson
  • 2,605
  • 2
  • 28
  • 51
Val
  • 207,596
  • 13
  • 358
  • 360
  • 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. – Tushar Khanna Aug 14 '15 at 12:19
  • 1
    Yes, you can do it as shown [here](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/script-filter.html), however, I'm not certain you can use scripts stored in files, only inlined ones. – Val Aug 14 '15 at 13:28
  • The hyperlink is broken – Ajay Takur Aug 23 '23 at 18:49
  • @AjayTakur this is very old stuff, I believe you described your issues in [this thread](https://stackoverflow.com/questions/76964218/unable-to-find-script-testfile-in-cluster-state-in-elastic-search-8-9-v) – Val Aug 23 '23 at 19:26