1

I'm trying to define some docs in ES Painless Lab, to test some logic in Painless Lab, before running it on the actual index, but can't figure out how to do it, and the docs are not helping either. There is very little documentation on the actual syntax and it's not much help for someone with no Java background.

If I try to define a doc like this:

def docs = [{ "id": 1, "name": "Apple" }];

I get an error:

Unhandled Exception illegal_argument_exception

invalid sequence of tokens near ['{'].

Stack:
[
  "def docs = [{ \"id\": 1, \"name\": \"Apple ...",
  "          ^---- HERE"
]

If I want to do it the Java way:

String message;
JSONObject json = new JSONObject();

json.put("test1", "value1");

message = json.toString();

I'm also getting an error:

Unhandled Exception illegal_argument_exception

invalid declaration: cannot resolve type [JSONObject]

Stack:
[
  "... ring message;\nJSONObject json = new JSONObject();\n ...",
  "                             ^---- HERE"
]

So what's the proper way to define an array of json objects to play with in Painless Lab?

orszaczky
  • 13,301
  • 8
  • 47
  • 54

1 Answers1

0

After more experimenting, I found out that the docs can be passed in the parameters tab as:

{
  "docs": [
    { "id": 1, "name": "Apple" },
    { "id": 2, "name": "Pear" },
    { "id": 3, "name": "Pineapple" }
  ]
}

and then access it from the code as

def doc = params.docs[1];
return doc["name"];

I'd be still interested how to define an object or array in the code itself.

orszaczky
  • 13,301
  • 8
  • 47
  • 54
  • Here you can find sample code for [arrays](https://www.elastic.co/guide/en/elasticsearch/painless/7.14/painless-types.html#array-type) and [maps/objects](https://www.elastic.co/guide/en/elasticsearch/painless/7.14/painless-operators-reference.html#map-initialization-operator) – Val Sep 15 '21 at 10:50