How to use painless loop to create a multidimensional array from values stored in 2 single arrays?

I'm having documents like this in kibana:

...
"children_relationship_type": [
"Blood",
"Blood",
"Adopted",
"Adopted"
],
"children_uuid": [
"a385a319",
"f27e7c95",
"e01ebd42",
"773622e4"
],
...

I want to create a painless scripted field from these 2 fields, with the following output:

"siblings": [
{"id": "a385a319","type": "Blood"},
{"id": "27e7c95","type": "Blood"},
{"id": "e01ebd42","type": "Adopted"},
{"id": "773622e4","type": "Adopted"}
],
I'm using this painless script:

def res="";
def i=0;
for(i=0; i < doc['sibling_uuid'].length;i++){
    res +='{"id": "'+doc['sibling_uuid'][i] + '","type": "' + doc['sibling_relationship_type'][i]+"},";
  }
  return res;

But this gives the following (wrong) output:

"siblings": [
{"id": "773622e4","type": "Blood"},{"id": "a385a319","type": "Adopted"},{"id": "e01ebd42","type": ""},{"id": "f27e7c95","type": "}
]

Can you help me?

Thanks. Frank

There is an error field names: read "sibling_relationship_type" instead of "children_relationship_type" and "sibling_uuid" instead of "children_uuid"

Hi @fbracq,

Is there a reason the data is stored in two separate arrays? It might be better to have them stored as siblings and have special handing when doing lookups in children_relationship_type and children_uuid.

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