1

I have the following code:

  "script": {
    "lang": "painless",
    "source": """
      ctx._source.maparray = [
    "first" : "Foo",
    "last" : "Bar"
]

which results in

"maparray": {
  "last": "Bar",
  "first": "Foo"
},

But I want maparray to be an array. So now based on:

https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-array.html

I try:

 "script": {
    "lang": "painless",
    "source": """
      ctx._source.maparray = new map[]{[
    "first" : "Foo",
    "last" : "Bar"
]}
    """,
    "params": {
      "key": "numWords"
    }
  }

but I get:

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "compile error",
        "script_stack": [
          "... x._source.maparray = new map[]{[\n    \"first\" : \"Fo ...",
          "                             ^---- HERE"
        ],
        "script": "      ctx._source.maparray = new map[]{[\n    \"first\" : \"Foo\",\n    \"last\" : \"Bar\"\n]}",
        "lang": "painless"
      }
    ],
    "type": "script_exception",
    "reason": "compile error",
    "script_stack": [
      "... x._source.maparray = new map[]{[\n    \"first\" : \"Fo ...",
      "                             ^---- HERE"
    ],
    "script": "      ctx._source.maparray = new map[]{[\n    \"first\" : \"Foo\",\n    \"last\" : \"Bar\"\n]}",
    "lang": "painless",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "invalid sequence of tokens near ['map'].",
      "caused_by": {
        "type": "no_viable_alt_exception",
        "reason": null
      }
    }
  },
  "status": 500
}

What is the issue with my syntax?

1 Answer 1

4

What you are looking for is actually an array of map. Below is how I've come up with a sample script where I've used Ingest Pipeline.

Required Pipeline with Script

PUT _ingest/pipeline/my-pipeline-id-01
{
  "description" : "describe pipeline",
  "processors" : [
    {
        "script" : {
          "lang" : "painless",
          "inline" : """
             ArrayList al = new ArrayList();
             Map map = new HashMap();
             map.put("first","Foo");
             map.put("last", "Bar");
             al.add(map);
             ctx.maparray = al;
            """
        }
      }
  ]
}

You can test how script works using Reindex API.

Reindex Script

POST _reindex
{
  "source": {
    "index": "<source_index_name>"
  },
  "dest": {
    "index": "<dest_index_name>",
    "pipeline": "my-pipeline-id-01"
  }
}

Please test the above, verify the results and let me know how it goes.

Hope this helps!

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

3 Comments

Was the above useful? Did it help!!?? Let me know if there's anything else you are looking for. Happy to help :)
hey @Mary, did the above help?
@Mary if the answer helped you, please accept it clicking on the green arrow next to it. This way other people will know that this is a useful answer for the question. It is also a good way to appreciate and thank the effort people put into helping you on this site.

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.