# Trouble Reindexing Child Documents with join field in ES 5.6

**URL:** <https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568>\
**Category:** Elasticsearch\
**Created:** [August 8, 2018, 5:29pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568 "2018-08-08T17:29:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mstruve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mstruve/32/50419_2.png) [@mstruve](https://discuss.elastic.co/u/mstruve)\
**Post date:** [August 8, 2018, 5:29pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/1 "2018-08-08T17:29:58Z")

</div>

My issue is similar to [this closed topic](https://discuss.elastic.co/t/how-to-mitigate-routing-is-missing-for-join-field-join-field/128145). I am trying to reindex parent/child documents in ES 5.6 in preparation for an ES 6.x upgrade. I was able to reindex the parent documents without a problem, however when I try to reindex the child documents I keep getting the error `[routing] is missing for join field`

Below is the code I am using to make the reindex call and as you can see the parent is being specified. I would also like to note that I am sure the mappings are set up correctly bc I was able to index child documents and they were properly joined to their parents. Reindexing is the only place I am having an issue. I think I might be missing something in the script but I am not sure what. Any help would be greatly appreciated.

```auto
POST _reindex
{
  "source": {
    "index":"development",
    "type": "vuln",
    "query": {
      "match_all": {}
    }
  },
  "dest": {
    "index": "a_mapping_test"
  },
  "script": {
    "source": "ctx._type='doc';ctx._source.asset_vuln_join=['name':'vuln', 'parent': ctx._parent];ctx.remove('_parent');"
  }
}

```

Error Response

```auto
{
      "index": "a_mapping_test",
      "type": "doc",
      "id": "123",
      "cause": {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse",
        "caused_by": {
          "type": "illegal_argument_exception",
          "reason": "[routing] is missing for join field [asset_vuln_join]"
        }
      },
      "status": 400
    }

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 9, 2018, 7:27am UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/2 "2018-08-09T07:27:54Z")

</div>

You need to set `ctx._routing` in your script. Set it equal to `ctx._parent` (before you `remove` it).

---

<div class="post-metadata">

**Author:** ![mstruve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mstruve/32/50419_2.png) [@mstruve](https://discuss.elastic.co/u/mstruve)\
**Post date:** [August 9, 2018, 1:25pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/3 "2018-08-09T13:25:00Z")

</div>

Tried that as well and I still get the same error. I tried setting it to `_parent` like below and I also tried setting it to `_routing` since that is already set in my current index. Both result in the same error as before.

```auto
  "script": {
    "source": "ctx._routing=ctx._parent;ctx._type='doc';ctx._source.asset_vuln_join=['name':'vulnerability', 'parent': ctx._parent];ctx.remove('_parent');"
  }

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 10, 2018, 12:24pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/4 "2018-08-10T12:24:22Z")

</div>

I'm wondering if some of your `vuln` type documents in the `development` index are missing the `_parent` field. That would explain this error. Can you post the document with the ID `123` that's causing the error that you are seeing?

---

<div class="post-metadata">

**Author:** ![mstruve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mstruve/32/50419_2.png) [@mstruve](https://discuss.elastic.co/u/mstruve)\
**Post date:** [August 10, 2018, 7:52pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/5 "2018-08-10T19:52:35Z")

</div>

Checked the docs in the development index and they all have parent and routing set correctly. I changed the id to 123 for simplicity but here is the actual doc and the error message together.

```auto
# Vuln Doc
{"_index"=>"development",
 "_type"=>"vuln",
 "_id"=>"18305772197390840",
 "_version"=>1,
 "_routing"=>"18305769100814537",
 "_parent"=>"18305769100814537",
 "found"=>true,
 "_source"=> ...

# ERROR
 {
      "index": "a_mapping_test",
      "type": "doc",
      "id": "18305772197390840",
      "cause": {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse",
        "caused_by": {
          "type": "illegal_argument_exception",
          "reason": "[routing] is missing for join field [asset_vuln_join]"
        }
      },
      "status": 400
    },

```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 11, 2018, 6:02pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/6 "2018-08-11T18:02:28Z")

</div>

I think you may be running into this issue: [https://github.com/elastic/elasticsearch/issues/26183](https://github.com/elastic/elasticsearch/issues/26183)

As a workaround, try giving the parent documents a different ID when you reindex those, for example `parent_18305769100814537` instead of just `18305769100814537`. Next, change your script to use that new ID to route the child documents:

```auto
"script": {
    "source": "ctx._routing='parent_'+ ctx._parent;ctx._type='doc';ctx._source.asset_vuln_join=['name':'vulnerability', 'parent': 'parent_' + ctx._parent];ctx.remove('_parent');"
  }

```

I think this will resolve the problem.

---

<div class="post-metadata">

**Author:** ![mstruve](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mstruve/32/50419_2.png) [@mstruve](https://discuss.elastic.co/u/mstruve)\
**Post date:** [August 11, 2018, 6:46pm UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/7 "2018-08-11T18:46:37Z")

</div>

Yep, that did the trick! Now I just need to figure out the best way to flip back to the original IDs once everything is reindexed since `update_by_query` doesn't work with `_routing`, whomp whomp.

Thanks for the quick answers!!!

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [August 12, 2018, 8:24am UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/8 "2018-08-12T08:24:15Z")

</div>

You could alway reindex the documents again, to another index, and doing so flip back the original parent IDs.

---

<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 9, 2018, 8:36am UTC](https://discuss.elastic.co/t/trouble-reindexing-child-documents-with-join-field-in-es-5-6/143568/9 "2018-09-09T08:36:22Z")

</div>

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