# 【ingest node】enrich processorに関して

**URL:** <https://discuss.elastic.co/t/ingest-node-enrich-processor/246571>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [August 27, 2020, 7:46am UTC](https://discuss.elastic.co/t/ingest-node-enrich-processor/246571 "2020-08-27T07:46:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harue](https://avatars.discourse-cdn.com/v4/letter/h/82dd89/32.png) [@harue](https://discuss.elastic.co/u/harue)\
**Post date:** [August 27, 2020, 7:46am UTC](https://discuss.elastic.co/t/ingest-node-enrich-processor/246571/1 "2020-08-27T07:46:41Z")

</div>

お世話になります。

ingest nodeのenrich processorに関して、質問させて頂きます。  
（下記問い合わせに関連したご質問になります。）

> [@【ingest node】別のindexのデータを登録する方法について](https://discuss.elastic.co/t/ingest-node-index/245375/4):
>
> Ingest Nodeですと、Enrich Processorがその機能になるかと思います。 概要はこちら [https://www.elastic.co/guide/en/elasticsearch/reference/7.9/ingest-enriching-data.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/ingest-enriching-data.html) 今回の例に近い合致したデータを補完するときの参考 [https://www.elastic.co/guide/en/elasticsearch/reference/7.9/match-enrich-policy-type.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/match-enrich-policy-type.html) match-enrich-policy-type.htmlの方を確認いただくとイメージがわくと思います。 こちらで確認したときの手順を以下に書いて終わります。 補完用のデータの準備 POST index\_c/\_doc/a { "a\_id": "1111", "b\_id": "aaaa" } POST index\_b/\_doc/a { "b\_id": "aaaa", "b\_comment": "test" } Enrich Policyの作成 # i…

▼実現したいこと

A indexの投入データの「A\_id」が"1111"の場合

①C index  
「A\_id」の値を条件に、「B\_id」の値( **複数有** )を取得

| C index | | |
| --- | --- | --- |
| \_id | A\_id | B\_id |
| 1 | 1111 | aaaa |
| 2 | 1111 | bbbb |

➁B index  
①で取得した「B\_id」の値( **複数有** )を条件に、各値に紐づく「B\_comennt」の値を取得

| B index | | |
| --- | --- | --- |
| \_id | B\_id | B\_comennt |
| 1 | aaaa | test1 |
| 2 | bbbb | test2 |

③A index  
「A\_id」,②で取得した「B\_id」,「B\_comennt」を登録  
（「B\_id」,「B\_comennt」はarrayで1ドキュメントで登録）

| A index | | | |
| --- | --- | --- | --- |
| \_id | A\_id | B\_id | B\_comennt |
| 1 | 1111 | aaaa,bbbb | test1,test2 |

▼検証

補完用のデータの準備

```
POST index_c/_doc/1
{
  "a_id": "1111",
  "b_id": "aaaa"
}

POST index_c/_doc/2
{
  "a_id": "1111",
  "b_id": "bbbb"
}

POST index_b/_doc/1
{
  "b_id": "aaaa",
  "b_comment": "test1"
}

POST index_b/_doc/2
{
  "b_id": "bbbb",
  "b_comment": "test2"
}

```

Enrich Policyの作成

```
# index_cに対してd_idをもとに、f_idを付与する
PUT /_enrich/policy/index-c-policy
{
  "match": {
    "indices": "index_c",
    "match_field": "a_id",
    "enrich_fields": ["b_id"]
  }
}

# index_bに対してb_idをもとに、b_commentを付与する
PUT /_enrich/policy/index-b-policy
{
  "match": {
    "indices": "index_b",
    "match_field": "b_id",
    "enrich_fields": ["b_comment"]
  }
}

```

PolicyのExecuteの実行

```
POST /_enrich/policy/index-c-policy/_execute
POST /_enrich/policy/index-b-policy/_execute

```

Ingest Pipelineの作成

```
PUT /_ingest/pipeline/test
{

  "processors": [
    {
      "enrich": {
        "policy_name": "index-c-policy",
        "field": "a_id",
        "target_field": "b",
        "max_matches": "2"
      }
    },
    {
      "enrich": {
        "policy_name": "index-b-policy",
        "field": "b.b_id", ←★
        "target_field": "b",
        "max_matches": "2"
      }
    },
    { 
      "rename": {
        "field": "b.b_comment",
        "target_field": "b_comment"
      }
    },
    {
      "rename": {
        "field": "b.b_id",
        "target_field": "b_id"
      }
    },
    {
      "remove": {
        "field": "b"
      }
    }
  ]
}

```

データ投入

```
PUT /my-index-00001/_doc/1?pipeline=test
{
  "a_id": "1111"
}

```

データ投入後、下記エラーが発生

```
{
  "error" : {
    "root_cause" : [
      {
        "type" : "illegal_argument_exception",
        "reason" : "[b_id] is not an integer, cannot be used as an index as part of path [b.b_id]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "[b_id] is not an integer, cannot be used as an index as part of path [b.b_id]",
    "caused_by" : {
      "type" : "number_format_exception",
      "reason" : "For input string: \"b_id\""
    }
  },
  "status" : 400
}

```

エラーの原因は上記★箇所であり、"b"の値が以下形式になっているためですが、

```
"b": [
  {
    "a_id": "1111",
    "b_id": "aaaa"
  },
  {
    "a_id": "1111",
    "b_id": "bbbb"
  }
],

```

正しく動作するためには、どのような記述をすれば良いでしょうか。  
（そもそもですが、enrich processorで複数の値を元に紐づけることは可能でしょうか。）

お手数ですが、回答を頂けますと幸いです。  
以上、宜しくお願い致します。

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [August 27, 2020, 3:00pm UTC](https://discuss.elastic.co/t/ingest-node-enrich-processor/246571/2 "2020-08-27T15:00:46Z")

</div>

enrichの1回目で取得された結果がarrayになっているため、2回目のenrichのb.b\_idがうまく取れないことが原因かと思います。

enrichを複数回、しかも結果が複数になるパターンが組み合わさっている前提の方を変えられないでしょうか？

具体的にいうと、今回のindex\_b, index\_cをみて、予めindex\_dのようなものを作っておきます。  
（index\_cに対して、b\_commentを補完するようなpipelineを設定してreindexすればindex\_dが作れます）  
そうすることで、enrichの回数を1回減らせて、結果的に求めたい結果に近くのではと思います。

作成されるindex\_dの中身の例

```json
         {
          "b" : [
            {
              "b_id" : "aaaa",
              "b_comment" : "test1"
            }
          ],
          "a_id" : "1111",
          "b_id" : "aaaa"
        }

```

このindex\_dに対して作成したenrich policyを使うのはどうでしょうか。

```auto
PUT /_enrich/policy/index-d-policy
{
  "match": {
    "indices": "index_d",
    "match_field": "a_id",
    "enrich_fields": ["b_id", "b.b_comment"]
  }
}

POST /_enrich/policy/index-d-policy/_execute

```

index-dのPolicyを使ってindexするようにする

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "enrich": {
          "policy_name": "index-d-policy",
          "field": "a_id",
          "target_field": "age",
          "max_matches": "20"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "aaa",
      "_id": "id1",
      "_source": {
        "a_id": "1111",
        "@timestamp": "2020-02-06T06:44:33.758Z"
      }
    }
  ]
}

```

このような感じでtest1、test2の値まで入ることが確認できます。

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/a/ba36af9a57959afe1033b0bb637aa11805832654.png)

もし、b\_commentだけを抜き出したいということであれば、scriptを使うと抽出もできそうです。

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/d/fdcc39374f03a02bd41f13e0a66c824875870dbd.png)

ご参考になれば。

---

<div class="post-metadata">

**Author:** ![harue](https://avatars.discourse-cdn.com/v4/letter/h/82dd89/32.png) [@harue](https://discuss.elastic.co/u/harue)\
**Post date:** [August 31, 2020, 1:37am UTC](https://discuss.elastic.co/t/ingest-node-enrich-processor/246571/3 "2020-08-31T01:37:40Z")

</div>

返信が遅くなり申し訳ありません。

回答頂きありがとうございます。  
上記の案を参考にさせて頂きます。

---

<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:** [September 28, 2020, 1:37am UTC](https://discuss.elastic.co/t/ingest-node-enrich-processor/246571/4 "2020-09-28T01:37:50Z")

</div>

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