0

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?

Mary
  • 1,005
  • 2
  • 18
  • 37

1 Answers1

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!

Kamal Kunjapur
  • 8,547
  • 2
  • 22
  • 32
  • Was the above useful? Did it help!!?? Let me know if there's anything else you are looking for. Happy to help :) – Kamal Kunjapur Apr 27 '19 at 10:33
  • hey @Mary, did the above help? – Kamal Kunjapur May 10 '19 at 11:13
  • @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. – sox supports the mods Aug 27 '20 at 12:44