# Parent-child relationshipが含まれるindexをreindex apiで複製しようとするとエラーになる件

**URL:** <https://discuss.elastic.co/t/parent-child-relationship-index-reindex-api/136242>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [June 18, 2018, 6:10am UTC](https://discuss.elastic.co/t/parent-child-relationship-index-reindex-api/136242 "2018-06-18T06:10:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yn2018](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yn2018](https://discuss.elastic.co/u/yn2018)\
**Post date:** [June 18, 2018, 6:10am UTC](https://discuss.elastic.co/t/parent-child-relationship-index-reindex-api/136242/1 "2018-06-18T06:10:04Z")

</div>

my\_indexの中にparent-child relationshipで親子関係を持ったtype my\_parentとtype my\_childがあり、このindexをreindex apiで複製しようとするとエラーになります。

```auto
$ curl -XPOST localhost:9200/_reindex?pretty -d '{
"source": {
  "index": "my_index"
},
"dest": {
  "index": "my_index_new"
}
}'

```

以下のようなエラーが多数出ます。

```auto
...
    {
      "index" : "my_index_new",
      "type" : "my_child",
      "id" : "114713",
      "cause" : {
        "type" : "illegal_argument_exception",
        "reason" : "can't specify parent if no parent field has been configured"
      },
      "status" : 400
    },
...

```

これを解決する方法はありますでしょうか？

elasticsearchのバージョンは5.6です。

---

<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:** [June 18, 2018, 8:08am UTC](https://discuss.elastic.co/t/parent-child-relationship-index-reindex-api/136242/2 "2018-06-18T08:08:19Z")

</div>

根本解決ではないかもしれませんが、こうしたらできた、という一例です。  
reindexの前に、移行先のmappingを作成しておくことでエラーがでずにreindexができました。

以下、こちらで試した内容です。（Elasticsearch 5.6.0使用）

## 移行元のindexとテストデータの作成

```auto
PUT test-index
{
  "mappings": {
    "my_child": {
      "_parent": {
        "type": "my_parent"
      },
      "_routing": {
        "required": true
      },
      "properties": {
        "value": {
          "type": "text"
        }
      }
    },
    "my_parent": {
      "properties": {
        "title": {
          "type": "text"
        }
      }
    }
  }
}

PUT test-index/my_parent/1
{
  "title": "this is a parent test."
}

```

## 移行先のMappingを事前作成

GET test-index/\_mappingの結果から作っただけです。

```auto
PUT new-index
{
  "mappings": {
    "my_child": {
      "_parent": {
        "type": "my_parent"
      },
      "_routing": {
        "required": true
      },
      "properties": {
        "value": {
          "type": "text"
        }
      }
    },
    "my_parent": {
      "properties": {
        "title": {
          "type": "text"
        }
      }
    }
  }
}

```

## reindexの実行

reindexされたことを確認するために、valueの値を変更するscriptをつけてみました。

```auto
POST _reindex
{
  "source": {
    "index": "test-index"
  },
  "dest": {
    "index": "new-index"
  },
  "script": {
    "inline": """
    if (ctx._source.value != null) {
       ctx._source.value = ctx._source.value + "_reinded";
    }
    """
  }
}

```

これだと正常に終了します。

```auto
#! Deprecation: Deprecated field [inline] used, expected [source] instead
{
  "took": 45,
  "timed_out": false,
  "total": 2,
  "updated": 0,
  "created": 2,
  "deleted": 0,
  "batches": 1,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

```

## 期待通りにreindexが終ったかを検索する

```auto
POST new-index/_search
{
  "query": {
    "has_child": {
      "type": "my_child",
      "min_children": 1,
      "query": {
        "match_all": {}
      },
      "inner_hits": {}
    }
  }
}

```

とやると、こんな感じで、reindexがちゃんと走っていることが確認できました。

```auto
(中略)
  "hits": [
                {
                  "_type": "my_child",
                  "_id": "2",
                  "_score": 1,
                  "_routing": "1",
                  "_parent": "1",
                  "_source": {
                    "value": "child value_reinded"
                  }
                }
              ]

```

---

<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:** [July 16, 2018, 8:08am UTC](https://discuss.elastic.co/t/parent-child-relationship-index-reindex-api/136242/3 "2018-07-16T08:08:25Z")

</div>

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