# Elastic Ingest Pipeline with enrich processor to enrich nested objects

**URL:** <https://discuss.elastic.co/t/elastic-ingest-pipeline-with-enrich-processor-to-enrich-nested-objects/253044>\
**Category:** Elasticsearch\
**Created:** [October 22, 2020, 5:12pm UTC](https://discuss.elastic.co/t/elastic-ingest-pipeline-with-enrich-processor-to-enrich-nested-objects/253044 "2020-10-22T17:12:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgiordano](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@bgiordano](https://discuss.elastic.co/u/bgiordano)\
**Post date:** [October 22, 2020, 5:12pm UTC](https://discuss.elastic.co/t/elastic-ingest-pipeline-with-enrich-processor-to-enrich-nested-objects/253044/1 "2020-10-22T17:12:34Z")

</div>

What is the correct syntax for the enrich processor to access field for enrichment for an object within an array?

This works to get access to field in single object, as shown in the example provided. This is what I am starting from.

POLICY DEFINITION:

```auto
PUT /_enrich/policy/country_enrichment_v1
{ 
  "match": { 
    "indices": [ 
      "country_enrichment_test"
    ], 
    "match_field": "countryName",
    "enrich_fields": [
      "continent", 
      "region_un", 
      "subregion", 
      "region_wb"
    ] 
  } 
}

```

EXECUTE POLICY and  
DEFINE INGEST PIPELINE WITH ENRICH PROCESSOR

```auto
PUT _ingest/pipeline/enrich_country_data_v0
{ 
  "processors": [ 
    { 
      "enrich": {
        "policy_name": "country_enrichment_v1", 
        "field": "nationOfResidence.name", 
        "target_field": "nationOfResidenceEnriched" 
      } 
    }
  ] 
}

```

INSERT RECORD:

```auto
PUT /reports_enriched_v0/_doc/2?pipeline=enrich_country_data_v0
{
  "nationOfResidence": {
    "name": "Singapore"
  }
}

```

\*\*\* Note actual object has additional fields \*\*\*

RESULT:

```auto
{
  "_index" : "reports_enriched_v0",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "nationOfResidence" : {
      "name" : "Singapore"
    },
    "nationOfResidenceEnriched" : {
      "continent" : "Asia",
      "subregion" : "Eastern Asia",
      "region_wb" : "Eastern Asia",
      "region_un" : "Asia",
      "countryName" : "Singapore"
    }
  }
}

```

==============================================================  
I want to do the same for same object field that is within an array.  
I assume I want to wrap the Enrich processor within a Foreach processor?

If so , what is the correct syntax to permit the enrich processor to access the match field in the object?

DEFINE INGEST PIPELINE WITH ENRICH PROCESSOR

```auto
PUT _ingest/pipeline/enrich_country_data_v1 
{ 
  "processors": [ 
    { 
      "enrich": {
        "policy_name": "country_enrichment_v1", 
        "field": "nationOfResidence.name", 
        "target_field": "nationOfResidenceEnriched" 
      } 
    },
    { 
      "foreach": {
        "field": "nationsTravelled",
        "processor": {
          "enrich": {
            "policy_name": "country_enrichment_v1", 
            "field": "name", <<== WHAT SHOULD GO HERE? I have tried a number of things but no success yet
            "target_field": "nationsTravelledEnriched" 
          } 
        }
      }
    }
  ] 
}

```

INSERT RECORD WITH LIST OBJECTS:

```auto
PUT /reports_enriched_v0/_doc/2?pipeline=enrich_country_data_v1
{
  "nationOfResidence": {
    "name": "Singapore"
  },
"nationsTravelled": [
  {
    "name": "Japan",
     ...
  },
  {
    "name": "Germany",
    ....
  }
]
}

```

\*\*\* NOTE: Actual objects have additional fields defined \*\*\*

Recommendations? Thank you.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 26, 2020, 9:26am UTC](https://discuss.elastic.co/t/elastic-ingest-pipeline-with-enrich-processor-to-enrich-nested-objects/253044/2 "2020-10-26T09:26:39Z")

</div>

try `_ingest._value.name` as mentioned [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/foreach-processor.html)

---

<div class="post-metadata">

**Author:** ![bgiordano](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@bgiordano](https://discuss.elastic.co/u/bgiordano)\
**Post date:** [October 26, 2020, 1:36pm UTC](https://discuss.elastic.co/t/elastic-ingest-pipeline-with-enrich-processor-to-enrich-nested-objects/253044/3 "2020-10-26T13:36:39Z")

</div>

Thanks - I did find the same solution for accessing a field in an object. Used _foreach_ processor to walk list of objects and then _\_ingest.value.name_ to access a field.

# Current Problem

Bigger issue that I have not found solution for is how to reach into an array within an array and get get an attribute for an object.

Below

# SubPipeline Definition

This sub pipeline is called from the main pipeline defined in the simulation below. What I want to reach in _\_source_ is the list of _namedItems.country.name_

Using the _foreach_ processor in the main pipeline on the _namedItems_ array and sending to subpipeline, seems to get me access to array of entries in _namedItems.country_.

What I can not seem to find the correct way to reach is the _name_ attribute for each _namedItems.country_

IDEAS?

```auto
PUT _ingest/pipeline/namedItemsSubPipeline 
{
  "processors": [
    {
      "append": {
        "field": "namedItemsCountries",
        "value": ["{{_ingest._value}}"]
      }
    },
    {
      "foreach": {
        "field": "namedItemsCountries",
        "processor": {
          "append": {
            "field": "namedItemsCountryNames",
            "value": ["{{_ingest._value.name}}"]
          }
        }
      }
    },
    { 
      "enrich": {
        "policy_name": "countries_with_enrichment", 
        "field": "namedItemsCountryNames", 
        "target_field": "namedItemsCountriesEnriched",
        "max_matches": 10
      } 
    }
  ]
}

```

# Simulate Main Pipeline

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [ 
      { 
        "enrich": {
          "policy_name": "countries_with_enrichment", 
          "field": "nationOfResidence.name", 
          "target_field": "nationOfResidenceEnriched" 
        } 
      },
      {
        "foreach": {
          "field": "nationsTravelled",
          "processor": {
            "append": {
              "field": "nationsTravelledCountryNames",
              "value": ["{{_ingest._value.name}}"]
            }
          }
        }
      },
      { 
        "enrich": {
          "policy_name": "countries_with_enrichment", 
          "field": "nationsTravelledCountryNames", 
          "target_field": "nationsTravelledEnriched",
          "max_matches": 10
        } 
      },
      {
        "foreach": {
          "field": "namedItems",
          "processor": {
            "pipeline": {
              "name": "namedItemsSubPipeline"
            }
          }
        }
      }
    ] 
  },
  "docs": [
    {
      "_source": {
        "nationOfResidence": {
          "name": "Canada",
          "shortCode": "CA"
        },
        "nationsTravelled": [
          {
            "name": "United States of America",
            "shortCode": "US"
          },
          {
            "name": "Great Britain",
            "shortCode": "GB"
          }
        ],
        "namedItems": [
          {
            "itemName": "Item 1",
            "country": {
              "name": "Mexico",
              "shortCode": "MX"
            }
          },
          {
            "itemName": "Item 2",
            "country": {
              "name": "Australia",
              "shortCode": "AU"
            }
          }
        ]
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 23, 2020, 1:36pm UTC](https://discuss.elastic.co/t/elastic-ingest-pipeline-with-enrich-processor-to-enrich-nested-objects/253044/4 "2020-11-23T13:36:41Z")

</div>

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