Painless script that takes values from nested object and turns it into a field

I Have a customFields nested object

             "customFields" : [
            {
              "description" : "Description",
              "Name" : "Document Author"
            },
            {
              "description" : "This is a description",
              "Name" : "document name"
            }
]

I want to transform this into single fields like this:

    "Document Author" :  "Description",
    "Document name" : "This is a description"

When i run my painless script in kibana:

    POST /new_document-20_v4/_update_by_query
{
  "script": {
    "inline": "\n      def source_copy = ctx._source;\n      def customFields = source_copy.remove('customFields');\n      \n      for (int i = 0; i < customFields.length; i++) {\n        // store the current iteratee\n        def current = customFields[i];\n        \n        // remove AND return the name\n        def name = current.remove('Name'); \n def desc = current.remove('descripion');  \n        \n        // set in the _source\n      source_copy[name] = desc;\n      }\n      \n      // replace the original source completely\n      ctx._source = source_copy;\n    "
,
    "query": {
      "bool": {
        "must": [
          {
            "exists": {
              "field": "customFields.Name"
            }
          }
        ]
      }
    }
  }
}

It returns them as single fields, but with a null value.

"Document Author" : null,
"Document name" : null

I a little confused as to why this happens, i am a little new to elasticsearch and painless scripting in general so if anyone can help me out it would be greatly appreciated!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.