Search code examples
elasticsearchelasticsearch-painless

Elasticsearch pipeline to extract fields of an object to a root of document


I'm looking for a way to extract the contents of an object such as

{
    "mdc":{
      "key1": "value1",
      "key2": "value2",
      ...
    }
}

and transform that into

{
       "key1": "value1",
       "key2": "value2",
       ...
        "mdc":{
          "key1": "value1",
          "key2": "value2"
        }
}

I was looking at the provided processors but couldn't find anything useful.

My initial thought was to:

  • specify a field whose contents I can regex match or select in such another way
  • iterate over them
  • inline the their contents to new fields.

Any suggestions would be greatly appreciated!


Solution

  • It wasn't that hard after all.

       {
          "mdcflatten": {
            "processors": [
              {
                "script": {
                  "lang": "painless",
                  "inline": " ctx.mdc.keySet().each (key -> ctx[key] = ctx.mdc.get(key))"
                }
              },
              {
                "remove": {
                  "field": "mdc"
                }
              }
            ]
          }
    

    Hope this helps.