# What to chose : Parent/child or Nested model

**URL:** https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959
**Category:** Elasticsearch
**Created:** [July 4, 2019, 5:34pm UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959 "2019-07-04T17:34:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Sunny\_Pelletier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sunny_pelletier/32/49451_2.png) [@Sunny\_Pelletier](https://discuss.elastic.co/u/Sunny_Pelletier)
#### Post date: [July 4, 2019, 5:35pm UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/1 "2019-07-04T17:35:00Z")

</div>

Hello everyone,

I am new to Elasticsearch, and there seems to be some really good features in this tool

I have explored it for a few days, and I am currently trying to decide which model type would be preferable. I made a small example of the type of data I am working with. It is not exactly what I am working with, but close to it

![diagram](https://us1.discourse-cdn.com/elastic/original/3X/4/2/42dc59c911f9fc913b1869a01aba990388fbf492.jpeg)

Using this model as an example, I will be required to do queries like `find questions by text`, `find answers by text`, `find comments by text` and `get all questions that have comments`. So you see, my problem is that I don't really know how I could organise my data in Elasticsearch so that all those queries would be efficient.

Would it be better to use a nested object model or a parent/child model ?

Thank you for your help and have a great day !

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 4, 2019, 7:25pm UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/2 "2019-07-04T19:25:01Z")

</div>

What about something like:

```auto
{
  "type": "answer",
  "text": "foo bar"
}
{
  "type": "question",
  "text": "foo bar"
}
{
  "type": "comment",
  "text": "foo bar"
}

```

---

<div class="post-metadata">

### Author: ![Sunny\_Pelletier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sunny_pelletier/32/49451_2.png) [@Sunny\_Pelletier](https://discuss.elastic.co/u/Sunny_Pelletier)
#### Post date: [July 5, 2019, 3:01am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/3 "2019-07-05T03:01:44Z")

</div>

That's pretty much what the parent/child is but there is a link with the parent using a joint.

I think that I found a way to do it this without having a multi level joint.

Thank you !

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 5, 2019, 6:53am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/4 "2019-07-05T06:53:22Z")

</div>

I'd not use parent/child unless I can't find any other way to do it. My example does not use parent/child.

---

<div class="post-metadata">

### Author: ![Miguel1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel1/32/82094_2.png) [@Miguel1](https://discuss.elastic.co/u/Miguel1)
#### Post date: [July 5, 2019, 7:21am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/5 "2019-07-05T07:21:00Z")

</div>

@dadoonet is right, as far as Elastic is for search (relevance and speed) it is better to denormalize as much as you can prior to use nested or parent/child relations.  
For instance, nested types are stored as different Lucene objects and they are put together in query time (it has a high cost).

---

<div class="post-metadata">

### Author: ![Sunny\_Pelletier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sunny_pelletier/32/49451_2.png) [@Sunny\_Pelletier](https://discuss.elastic.co/u/Sunny_Pelletier)
#### Post date: [July 5, 2019, 11:05am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/6 "2019-07-05T11:05:13Z")

</div>

Oh okay sorry I misunderstood your answer then.

I understand that I have to denormalize my data and they actually are already denormalized. The problem is that I have to link them together somewhere. Would it be more efficient to have them linked already in the index or to do multiple queries?

Also, with your model, is it possible to query for "answers that have comments"?

Thank you for your time!

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [July 5, 2019, 11:21am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/7 "2019-07-05T11:21:30Z")

</div>

> [@Sunny\_Pelletier](#):
>
> The problem is that I have to link them together somewhere. Would it be more efficient to have them linked already in the index or to do multiple queries?

Yes. Join at index time is better if you want to have fast response and you want to minimize the Java HEAP usage.

> [@Sunny\_Pelletier](#):
>
> Also, with your model, is it possible to query for "answers that have comments"?

I'm not sure. You should share some examples about what you want to manage exactly. Like document samples in the same way I wrote my examples.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [July 5, 2019, 11:47am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/8 "2019-07-05T11:47:51Z")

</div>

A lot depends on how you want to query the data, data sizes, frequencies of update etc.

I have a [decision flowchart](https://discuss.elastic.co/t/indexing-hierarchical-couchbase-documents/70400/2) that might help

---

<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: [August 2, 2019, 11:47am UTC](https://discuss.elastic.co/t/what-to-chose-parent-child-or-nested-model/188959/9 "2019-08-02T11:47:54Z")

</div>

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