# Is searching by \_id faster in parent-child relationship?

**URL:** https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633
**Category:** Elasticsearch
**Created:** [November 20, 2016, 5:18am UTC](https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633 "2016-11-20T05:18:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![shadyabhi](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shadyabhi](https://discuss.elastic.co/u/shadyabhi)
#### Post date: [November 20, 2016, 5:18am UTC](https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633/1 "2016-11-20T05:18:08Z")

</div>

I'm trying to model one-to-many relationships in Elasticsearch with ability to fetch parents and children without involving any searches.

**Question 1:** I thought that the way I can achieve this is by using `_id` wherever possible. Can someone confirm if fetching a document by doing `GET` request to `/index/type/1` faster than doing a term query on a field that's not `_id`?

Below is an example of 1 parent and its 2 children.

Parent (type: parent):-

```
{
  "_id": "deterministic_id_1",
  "app": "awesome-app-1",
  "server": "awesome-server-1"
}

```

Children (type: children):-

```
{
  "config": "awesome-config-1"
  "_parent": "deterministic_id_1"
}
{
  "config": "awesome-config-2"
  "_parent": "deterministic_id_1"
}

```

I have deterministic \_id field so that I can fetch parent documents without searching. That already works, simply do a `GET` request for `/index/type/<id>`.  
I know that Elasticsearch maintains a mapping of all parent-\>child relationships internally. Now, how do I fetch all children for a parent without searching? Is there a way? Best I could come up with is:-

```
POST /index/children/_search
{
   "query": {
      "has_parent": {
          "type": "parent",
          "query": {
              "ids": {
                  "type": "config_parent",
                  "_id": "deterministic_id_1"
              }
          }
      }
   }
}

```

**Question 2:** Does Is this the fastest way I can fetch all children for the parent with `_id` as `deterministic_id_1`? Does this query hit all shards?

I want two things from my data:-

1. Get access to all parents using my deterministic\_id\_1. (already done)
2. Get access to all children using the parent's document id without involving any searching. (pending)

---

<div class="post-metadata">

### Author: ![shadyabhi](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shadyabhi](https://discuss.elastic.co/u/shadyabhi)
#### Post date: [November 23, 2016, 3:18pm UTC](https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633/2 "2016-11-23T15:18:05Z")

</div>

Anyone?

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [November 25, 2016, 3:15pm UTC](https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633/3 "2016-11-25T15:15:36Z")

</div>

> [@shadyabhi](#):
>
> Can someone confirm if fetching a document by doing GET request to /index/type/1 faster than doing a term query on a field that's not \_id?

It should perform a bit better but I would not expect it to be a game changer.

> [@shadyabhi](#):
>
> Question 2: Does Is this the fastest way I can fetch all children for the parent with \_id as deterministic\_id\_1? Does this query hit all shards?

I think you should be able to do something like this:

```auto
POST /index/children/_search?routing=deterministic_id_1
{
   "query": {
     "term": {
       "_parent": "deterministic_id_1"
     }
   }
}

```

---

<div class="post-metadata">

### Author: ![shadyabhi](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@shadyabhi](https://discuss.elastic.co/u/shadyabhi)
#### Post date: [November 25, 2016, 3:31pm UTC](https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633/4 "2016-11-25T15:31:37Z")

</div>

Hi @jpountz,

Thanks for the reply. I somehow missed query as one of the options.

I see that we're using a routing key. Can I safely assume that this is the fastest way to search all children for a parent?

---

<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: [December 23, 2016, 3:31pm UTC](https://discuss.elastic.co/t/is-searching-by--id-faster-in-parent-child-relationship/66633/5 "2016-12-23T15:31:39Z")

</div>

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