0

I am trying to run a script field query. i have placed the following script in the config/scripts folder

1 + my_var

rwxr-xr-x 3 elasticsearch elasticsearch 4096 Jan  4 16:08 ..
-rwxrwxrwx 1 root          root           145 Jan  7 11:57 session-duration-script.groovy
-rwx------ 1 root          root           124 Jan  7 12:52 calc-session-duration-script.groovy
-rwxrwxrwx 1 root          root            11 Jan  7 13:09 my_script.groovy
drwxr-xr-x 2 elasticsearch elasticsearch 4096 Jan  7 13:09 .

mgmt/cm/access/access-event-logs/session-summary/_search

{
    "script_fields": {
        "my_field": {
            "file": "my_script",
            "params": {
              "my_var": 2
            }
        }
    }

}

Here is the error message

Caused by: SearchParseException[failed to parse search source [{
   "script_fields": {
        "my_field": {
            "file": "my_script",
            "params": {
              "my_var": 2
            }
        }

   }
}]]; nested: SearchParseException[must specify a script in script fields];
        at org.elasticsearch.search.SearchService.parseSource(SearchService.java:848)
        at org.elasticsearch.search.SearchService.createContext(SearchService.java:651)
        at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:617)
        at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:368)
        at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:368)
        at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:365)
        at org.elasticsearch.transport.TransportService$4.doRun(TransportService.java:350)
        at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: SearchParseException[must specify a script in script fields]
        at org.elasticsearch.search.fetch.script.ScriptFieldsParseElement.parse(ScriptFieldsParseElement.java:98)
        at org.elasticsearch.search.SearchService.parseSource(SearchService.java:831)
        ... 10 more
  1. Do I need to enable the groovy scripting for running the script_field query. From documentation I understand that ES cause execute scripts placed under /config/scripts with out enabling the scripting

1 Answers1

0

You just need to wrap the definition of your script field within a script section, like this:

  "script_fields": {
    "my_field": {
      "script": {                 <---- add this
        "file": "my_script",
        "params": {
          "my_var": 2
        }
      }
    }
  }

Also make sure to enable dynamic scripting from stored script files in your elasticsearch.yml configuration file (+restart your node):

script.file: on
Val
  • 207,596
  • 13
  • 358
  • 360
  • I just added "script": { line and it worked :) Thanks for the quick response !!!. Have another question . When i added "script_fields": { section the query the results returned only the field mentioned in the script_filelds section. Is there a way return all the fields in the document + script_fields – Karthikeyan Ramasamy Jan 07 '16 at 22:03
  • If you have few minutes , can you look at this question too http://stackoverflow.com/questions/34666720/elastic-search-partial-update – Karthikeyan Ramasamy Jan 08 '16 at 00:50