# Index documents with autogenerated ID and custom routing

**URL:** <https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [December 21, 2020, 4:34pm UTC](https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287 "2020-12-21T16:34:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maksym1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maksym1/32/78018_2.png) [@Maksym1](https://discuss.elastic.co/u/Maksym1)\
**Post date:** [December 21, 2020, 4:34pm UTC](https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287/1 "2020-12-21T16:34:09Z")

</div>

I want to index documents with an autogenerated ID and a Routing.  
The ID field is autogenerated, so I want to link a parent to a child by ExternalId.

I use NEST client for .NET and have such a piece of code:

```auto
 await _elasticClient.BulkAsync(b => b
                .IndexMany(estimateDocuments, (descriptor, estimate) => descriptor
                    .Routing(estimate.ExternalId)
                    .Id(estimate.Id)
                    .Index(_elasticSearch.EstimateIndex)
                )
                .Refresh(Elasticsearch.Net.Refresh.True)

                .IndexMany(estimateCustomers, (descriptor, customer) => descriptor
                    .Routing(customer.Parent)
                    .Id(customer.Id)
                    .Index(_elasticSearch.EstimateIndex)
                )
                .Refresh(Elasticsearch.Net.Refresh.True)
);

```

It's ok. But now, I can't get an estimate with a customer.  
Here is my query:

```auto
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "number": "5785"
                    }
                },
                {
                    "has_child": {
                        "type": "customer",
                        "query": {
                            "match_all": {}
                        },
                        "inner_hits": {}
                    }
                }
            ]
        }
    }
}

```

This is a result:

```auto
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.9808291,
        "hits": [
            {
                "_index": "estimate-index",
                "_type": "_doc",
                "_id": "SVwAhnYBf45xwI3rMoDn",
                "_score": 0.9808291,
                "_routing": "699f0ab9-248f-44f8-9192-000fe61fd2cd",
                "_source": {
                    "number": "5785",
                    "externalId": "699f0ab9-248f-44f8-9192-000fe61fd2cd",
                    "joinField": "estimate"
                },
                "inner_hits": {
                    "item": {
                        "hits": {
                            "total": {
                                "value": 0,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": []
                        }
                    },
                    "customer": {
                        "hits": {
                            "total": {
                                "value": 0,
                                "relation": "eq"
                            },
                            "max_score": null,
                            "hits": []
                        }
                    }
                }
            }
        ]
    }

```

---

<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:** [December 26, 2020, 10:15am UTC](https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287/2 "2020-12-26T10:15:13Z")

</div>

If you are using parent-child relationships the parent and child documents must reside in the same shard and therefore need to use the same routing value.

---

<div class="post-metadata">

**Author:** ![Maksym1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/maksym1/32/78018_2.png) [@Maksym1](https://discuss.elastic.co/u/Maksym1)\
**Post date:** [December 29, 2020, 10:05am UTC](https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287/3 "2020-12-29T10:05:57Z")

</div>

Both routings settings (ExternalId and Parent) are the same. And they have to be in the same shard.

---

<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:** [December 29, 2020, 11:14am UTC](https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287/4 "2020-12-29T11:14:29Z")

</div>

Please provide documents, mappings and queries required to reproduce this in the Kibana development console. I do not know the NEST client and am not a .NET developer.

---

<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:** [January 26, 2021, 11:14am UTC](https://discuss.elastic.co/t/index-documents-with-autogenerated-id-and-custom-routing/259287/5 "2021-01-26T11:14:41Z")

</div>

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