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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Here you can find sample code for arrays and maps/objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.