# Searching nested objects with parent-child relationship

**URL:** <https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687>\
**Category:** Elasticsearch\
**Created:** [July 22, 2019, 5:03pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687 "2019-07-22T17:03:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slobbard](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Slobbard](https://discuss.elastic.co/u/Slobbard)\
**Post date:** [July 22, 2019, 5:03pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/1 "2019-07-22T17:03:34Z")

</div>

Here's the mapping:

```
{
 "mappings":{
  "_doc":{
   "properties":{
    "person":{
     "type": "nested",
     "properties":{
      "name": {
       "type": "text"
      }
     }
    },
    "item": {
     "type": "nested",
     "properties": {
      "label":{
       "type":"text"
      },
      "description":{
       "type":"text"
      }
     }
    },
    "owner": {
     "type": "join",
     "relations": {
      "person": "item"
     }
    }
   }
  }
 }
}

```

So then I create a person:

```
XPUT /_doc/1?refresh
{
 "person": {
  "name": "John Smith"
 },
 "owner": "person"
}

```

And an item:

```
XPUT /_doc/1-1?routing=1
{
 "item": {
  "label": "Bag",
  "description": "Used to carry around stuff"
 },
 "owner": {
  "name": "item",
  "parent": 1
 }
}

```

Now, I want to query ES to fetch both the item and the owner of that item (not just the id, but also all the owner's fields).  
I've already tried using _nested queries_ with `has_parent` filters, or `has_parent` queries with a _nested query_ within it, but I always get no results.

Any ideas on how I can do that?

---

<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 23, 2019, 10:43am UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/2 "2019-07-23T10:43:10Z")

</div>

Hi Slobbard,

`nested` and `parent/child` are 2 different approaches to joins.

- Nested is about storing related content in the same JSON but storing as neighbouring Lucene documents in an index.
- Parent/child is about storing parents and children in different JSON docs but routing them to the same shard.

Each has different indexing and query apis accordingly.

---

<div class="post-metadata">

**Author:** ![Slobbard](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Slobbard](https://discuss.elastic.co/u/Slobbard)\
**Post date:** [July 23, 2019, 12:25pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/3 "2019-07-23T12:25:58Z")

</div>

Hi Mark, thanks for your reply.

Should I change my mapping or my query then?  
Is there a way to query children and fetch also parents' data?

---

<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 23, 2019, 12:50pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/4 "2019-07-23T12:50:13Z")

</div>

> [@Slobbard](#):
>
> Should I change my mapping or my query then?

You need to decide if you want parent/child or nested (this [flowchart](https://discuss.elastic.co/t/indexing-hierarchical-couchbase-documents/70400/2) can help) then ensure that your indexing and querying use the appropriate parameters for that choice.

> [@Slobbard](#):
>
> Is there a way to query children and fetch also parents' data?

[Yep.](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html#_parent_join_queries_and_aggregations)

---

<div class="post-metadata">

**Author:** ![Slobbard](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Slobbard](https://discuss.elastic.co/u/Slobbard)\
**Post date:** [July 23, 2019, 1:17pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/5 "2019-07-23T13:17:22Z")

</div>

> [@Mark\_Harwood](#):
>
> You need to decide if you want parent/child or nested (this [flowchart](https://discuss.elastic.co/t/indexing-hierarchical-couchbase-documents/70400/2) can help) then ensure that your indexing and querying use the appropriate parameters for that choice.

So my mapping as described above is in fact incompatible with what I want to achieve..?  
From your flowchart I'd say that flattening everything into a single JSON structure would probably be best, and then querying for child-parent with a join.

My idea was to use a more encapsulated approach like the one above because person and item will have many more properties within them, and I figured it'd encapsulate better the two entities.  
Is a flattened JSON the recommended Elasticsearch approach?

---

<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 23, 2019, 1:30pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/6 "2019-07-23T13:30:16Z")

</div>

> [@Slobbard](#):
>
> I'd say that flattening everything into a single JSON structure would probably be best, and then querying for child-parent with a join.

You're confusing [bullet point 1 with 2](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/2) again

---

<div class="post-metadata">

**Author:** ![Slobbard](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Slobbard](https://discuss.elastic.co/u/Slobbard)\
**Post date:** [July 24, 2019, 12:52pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/7 "2019-07-24T12:52:02Z")

</div>

> [@Mark\_Harwood](#):
>
> You're confusing [bullet point 1 with 2](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/2) again

Hey Mark, thanks again for your replies.  
I am a little confused. You said that `nested` and `parent/child` are two different approaches to joins.  
I'm trying to make them work together, in fact, as I wanted to achieve what I've described above. Is there a way to make that work? What am I doing wrong exactly?

---

<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 24, 2019, 1:04pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/8 "2019-07-24T13:04:14Z")

</div>

> [@Slobbard](#):
>
> What am I doing wrong exactly?

It depends what you're trying to achieve. Looking at the mapping for example I can't see any reason why `person` is modelled as a `nested` property when a plain `object` type would suffice.  
You would typically only resort to `nested` when you have an array of JSON objects each with \>1 field and you want to avoid confusing properties of one object with that of another. For example, if a person has many `car` objects you might use `nested` if you wanted to query for `car.colour:blue` AND `car.make:Ford` to avoid confusing values of the blue BMW and the black Ford. If you only have one property (eg vehicleNumber) then there's no confusion and no need for nested. Your Person object is like that - it only has one property. This [old slide deck](https://www.slideshare.net/MarkHarwood/proposal-for-nested-document-support-in-lucene) explains the outline of why nested is sometimes required with Lucene.

---

<div class="post-metadata">

**Author:** ![Slobbard](https://avatars.discourse-cdn.com/v4/letter/s/b9bd4f/32.png) [@Slobbard](https://discuss.elastic.co/u/Slobbard)\
**Post date:** [July 24, 2019, 1:54pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/9 "2019-07-24T13:54:46Z")

</div>

Alright I see what you're trying to say, the example helped a lot, thanks Mark!

---

<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 21, 2019, 1:54pm UTC](https://discuss.elastic.co/t/searching-nested-objects-with-parent-child-relationship/191687/10 "2019-08-21T13:54:47Z")

</div>

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