# Nested data with two level

**URL:** <https://discuss.elastic.co/t/nested-data-with-two-level/83849>\
**Category:** Elasticsearch\
**Created:** [April 27, 2017, 11:27am UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849 "2017-04-27T11:27:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhishek5678](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abhishek5678](https://discuss.elastic.co/u/abhishek5678)\
**Post date:** [April 27, 2017, 11:27am UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849/1 "2017-04-27T11:27:35Z")

</div>

Hi Guys,  
In this data when i am trying with one level nested data then it's working fine but when i am adding one more nested data then it's not working if anybody will be knows about that then please reply me.I am sending my document data and mapping file also.

POST /test\_word14/doc/1  
{  
"name": "Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism (D50-D89)",  
"depth": 1,  
"children": [{  
"name": "Nutritional anemias (D50-D53)",  
"depth": 2,  
"children": [{  
"code": "D50.0",  
"name": "Iron deficiency anemia secondary to blood loss (chronic)",  
"depth": 4  
}, {  
"code": "D50.1",  
"name": "Sideropenic dysphagia",  
"depth": 4  
}, {  
"code": "D50.8",  
"name": "Other iron deficiency anemias",  
"depth": 4  
}, {  
"code": "D50.9",  
"name": "Iron deficiency anemia, unspecified",  
"depth": 4  
}]  
}]  
}

(mapping file)  
PUT /test\_word16  
{  
"mappings": {  
"doc": {  
"properties": {  
"name": { "type": "string" },  
"depth": {"type":"long"},  
"children": {  
"type": "nested",  
"properties": {  
"name": { "type": "string" },  
"depth": { "type": "long" },  
"children": {  
"type": "nested",  
"properties": {  
"code": { "type": "string" },  
"name": { "type": "string" },  
"depth": { "type": "long" },  
}  
}  
}  
}  
}  
}  
}  
}

please tell me what is the query i am applying here.i applied the query like this:  
GET /icd10\_codes/doc/\_search  
{  
"\_source": false,  
"query": {  
"nested": {  
"path": "children",  
"query": {  
"nested": {  
"path": "children.children",  
"query": {  
"nested": {  
"path": "children.children.children",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"[children.children.name](http://children.children.name)": "Iron"  
}  
}  
]  
}  
},  
"inner\_hits": {  
"\_source": {  
"excludes":["name"]  
}  
}  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [April 28, 2017, 12:49pm UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849/2 "2017-04-28T12:49:41Z")

</div>

So your search response did include hits but did not include inner hits?

Based on your search request, I think you need to define `inner_hits` on all nested queries, otherwise the inner hits of the most inner nested query don't get picked up.

---

<div class="post-metadata">

**Author:** ![abhishek5678](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abhishek5678](https://discuss.elastic.co/u/abhishek5678)\
**Post date:** [April 28, 2017, 1:07pm UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849/3 "2017-04-28T13:07:24Z")

</div>

I also define like that but still i am not getting  
GET /icd10\_codes/doc/\_search?\_source=false  
{  
"query": {  
"nested": {  
"path": "children",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"[children.name](http://children.name)": "Nutritional anemias (D50-D53)"  
}  
},  
{  
"nested": {  
"path": "children.children",  
"query": {  
"match": {  
"[children.children.name](http://children.children.name)": "Sideropenic dysphagia"  
}  
}  
}  
}  
]  
}  
}  
}  
},  
"inner\_hits": {  
"kids": {  
"path": {  
"children": {  
"query": {  
"match": {  
"[children.name](http://children.name)": "Nutritional anemias (D50-D53)"  
}  
},  
"inner\_hits": {  
"grand\_kids": {  
"path": {  
"children.children": {  
"query": {  
"match": {  
"[children.children.name](http://children.children.name)": "Sideropenic dysphagia"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![abhishek5678](https://avatars.discourse-cdn.com/v4/letter/a/5daacb/32.png) [@abhishek5678](https://discuss.elastic.co/u/abhishek5678)\
**Post date:** [April 28, 2017, 1:09pm UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849/4 "2017-04-28T13:09:12Z")

</div>

But when i am defining like that then it's showing:  
"type": "parsing\_exception",  
"reason": "Unknown key for a START\_OBJECT in [inner\_hits]

---

<div class="post-metadata">

**Author:** ![Nilabhsagar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nilabhsagar/32/10647_2.png) [@Nilabhsagar](https://discuss.elastic.co/u/Nilabhsagar)\
**Post date:** [May 3, 2017, 10:06am UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849/5 "2017-05-03T10:06:21Z")

</div>

You are not using inner hits as expected. Follow the [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html) documentation, this will help you.

---

<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:** [May 31, 2017, 10:16am UTC](https://discuss.elastic.co/t/nested-data-with-two-level/83849/6 "2017-05-31T10:16:19Z")

</div>

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