3

I want to write a script for script_score in elasticsearch.

In Painless Documentation there is a list of java classes under "Shared api reference".

GET hockey/_search
{
  "explain": true, 
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "total_goals": {
      "script": {
        "lang": "painless",
        "source": """

          int[] arr = new int[3];
          arr[0] = 1;
          arr[1] = 2;
          arr[2] = 3;
          return arr;

        """,
        "params":{
          "last" : "any parameters required"
        }

      }
    }
  }
}

Above script works as expected. But i want to use Java's ArrayList or some other Class instead.

GET hockey/_search
{
  "explain": true, 
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "total_goals": {
      "script": {
        "lang": "painless",
        "source": """
          ArrayList<Integer> al = new ArrayList<Integer>();
          al.add(1);
          al.add(2);
          return al;

        """,
        "params":{
          "last" : "any parameters required"
        }

      }
    }
  }
}

this throws following error.

{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "\n          ArrayList<Integer> al = new ArrayL ...",
          "                    ^---- HERE"
        ],
        "script" : "\n          ArrayList<Integer> al = new ArrayList<Integer>();\n          al.add(1);\n          al.add(2);\n          return al;\n          \n        ",
        "lang" : "painless"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "hockey",
        "node" : "UIMgEAZNRzmIpRGyQtNk9g",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "\n          ArrayList<Integer> al = new ArrayL ...",
            "                    ^---- HERE"
          ],
          "script" : "\n          ArrayList<Integer> al = new ArrayList<Integer>();\n          al.add(1);\n          al.add(2);\n          return al;\n          \n        ",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "invalid sequence of tokens near ['<'].",
            "caused_by" : {
              "type" : "no_viable_alt_exception",
              "reason" : null
            }
          }
        }
      }
    ]
  },
  "status" : 400
}

If it is possible to use ArrayList class then,

Do i have to import it, from java.util ?

Abhinav Keshri
  • 595
  • 5
  • 20

1 Answers1

2

You can use either ArrayList al = new ArrayList(); or even simply def al = new ArrayList(); More info in the docs.

FYI: you can make use of Debug.explain(al); to check what's what!

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68