# Ingest Processor for array object data using script

**URL:** https://discuss.elastic.co/t/ingest-processor-for-array-object-data-using-script/379375
**Category:** Elasticsearch
**Tags:** ingest-pipeline
**Created:** [June 20, 2025, 6:46pm UTC](https://discuss.elastic.co/t/ingest-processor-for-array-object-data-using-script/379375 "2025-06-20T18:46:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![adude946](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@adude946](https://discuss.elastic.co/u/adude946)
#### Post date: [June 20, 2025, 6:46pm UTC](https://discuss.elastic.co/t/ingest-processor-for-array-object-data-using-script/379375/1 "2025-06-20T18:46:30Z")

</div>

Need assist to get the Ingest pipeline processors to work correctly for my sample data. Its an array of objects, trying to create individual objects from each array object using a value data + prefix names based on these values.

**Input**

```auto
{
   "color_bag": [{
       "val": "g1",
       "name": "color_type1",
       "attb": {}
    },
    {
       "val": "g2",
       "name": "color_type2",
       "attb": {}
    },
    {
       "val": "g3",
       "name": "color_type3",
       "attb": {}
    }]    
}

```

**Output needed post processing**

```auto
{
  "2025_red_g1": {
    "val": "g1",
    "name": "color_type1",
    "attb": {}
  },
  "2025_green_g2": {
    "val": "g2",
    "name": "color_type2",
    "attb": {}
  },
  "2025_blue_g3": {
    "val": "g3",
    "name": "color_type3",
    "attb": {}
  }
}

```

**Logic in processor**

> - For each object in array color\_bag:
> - if color\_bag.val = 'g1': then object\_prefix = 'red\_'
> - if color\_bag.val = 'g2': then object\_prefix = 'green\_'
> - if color\_bag.val = 'g3': then object\_prefix = 'blue\_'
> 
> - Set: object\_name = '2025\_' + object\_prefix + color\_bag.val
> - Create new object as object\_name, in parent root object with full data as in the its array object.

**Sample Mapping**

```auto
{
  "mappings": {
    "properties": {
      "2025_red_g1": {"type": "object", "properties" : {
          "val" : {"type": "keyword"},
          "name" : {"type": "keyword"},
          "attb": {"type": "object", "properties" : {"k": {"type": "keyword"}}}
       }},
      "2025_green_g2": {"type": "object", "properties" : {
          "val" : {"type": "keyword"},
          "name" : {"type": "keyword"},
          "attb": {"type": "object", "properties" : {"k": {"type": "keyword"}}}
       }},
      "2025_blue_g3": {"type": "object", "properties" : {
          "val" : {"type": "keyword"},
          "name" : {"type": "keyword"},
          "attb": {"type": "object", "properties" : {"k": {"type": "keyword"}}}
       }}
    }
  }
}

```

I have a half baked script which i know is wrong as it keeps erroring. Cannot get this coded correctly.

```auto
{
  "processors": [
    {
      "foreach": {
        "field": "color_bag",
        "processor": {
          "script": {
            "source": """
              def prefix = "2025_";

              if (ctx._ingest._value.val = 'g1') {
                  temp_name = 'red_';
              }
              if (ctx._ingest._value.val = 'g2') {
                  temp_name = 'green_';
              }
              if (ctx._ingest._value.val = 'g3') {
                  temp_name = 'blue_';
              }              
              
              def object_prefix = prefix + temp_name + ctx._ingest._value.val;
              ctx[object_prefix] = ctx._ingest._value;
            """
          }
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [June 20, 2025, 6:52pm UTC](https://discuss.elastic.co/t/ingest-processor-for-array-object-data-using-script/379375/2 "2025-06-20T18:52:37Z")

</div>

That type of dynamic naming of fields can result in excessive number of mapped fields, aka mapping explosion, and is generally a quite bad idea. I would recommend not syructuring your data that way as it is likely to cause problems unless the number of permutations is quite limited.

---

<div class="post-metadata">

### Author: ![adude946](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@adude946](https://discuss.elastic.co/u/adude946)
#### Post date: [June 20, 2025, 7:14pm UTC](https://discuss.elastic.co/t/ingest-processor-for-array-object-data-using-script/379375/3 "2025-06-20T19:14:23Z")

</div>

In total there are up to 8 permutations only as the max.
