Script Filter: Groovy - doesn't return any results


(Shiv Naidu) #1

Hi All,

I am running in to a issue that I am not sure if I am not using it correctly or I need to enable something to get it to work. I had a simple query to that will take the value and checks if the value is greater than the given cutt off number. The data is stored as string, so in the script I am converting that in to an integer. I don't get any exception but I doesn't return any result, it returns with 0 hits. Please let me know what is wrong.

See my steps below for what I did so far.

Thanks,
-Shiv

Elastic Search Version: 1.7.2

  1. I enabled the groovy scripting in the elasticsearch.yml file, "script.disable_dynamic: false" and restarted the elastic server.

  2. Inserted the following records,

[
    {
        "firstName": "Miguel1",
        "lastName": "Atkins1",
        "age": "5"
    },
    {
        "firstName": "Miguel2",
        "lastName": "Atkins2",
        "age": "6"
    },
    {
        "firstName": "Miguel3",
        "lastName": "Atkins3",
        "age": "7"
    },
    {
        "firstName": "Migue4",
        "lastName": "Atkins4",
        "age": "abc"
    }
]
  1. Now I try to run the following query,
{
  "query": {
    "filtered": {
      "filter": {
        "script": {
          "script": "try{ if (Integer.parseInt(doc['age'].value) > cutoff) return true; else return false; } catch(Exception e){  return false; }",
          "lang": "groovy",
          "params": {
            "cutoff": 5
          }
        }
      },
      "query": {
        "match_all": {}
      }
    }
  }
}

(Shiv Naidu) #2

Found the solution :slightly_smiling:

I have add another configuration to run the script in its entirety not just inline.

Added the following config, "script.groovy.sandbox.enabled: true" and restarted the server and the query ran fine.

Thanks!


(system) #3