# Multiple self nesting

**URL:** <https://discuss.elastic.co/t/multiple-self-nesting/12399>\
**Category:** Elasticsearch\
**Created:** [June 13, 2013, 3:37pm UTC](https://discuss.elastic.co/t/multiple-self-nesting/12399 "2013-06-13T15:37:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11119](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/11119/32/2276_2.png) [@11119](https://discuss.elastic.co/u/11119)\
**Post date:** [June 13, 2013, 3:37pm UTC](https://discuss.elastic.co/t/multiple-self-nesting/12399/1 "2013-06-13T15:37:42Z")

</div>

I want to store hierarchical data into elasticsearch, for example:

root  
folder1  
file11  
file12  
folder2  
file21  
file22

I made it so every item has parent who is pointing on self type. For  
example:  
curl -XPOST localhost:9200/filesystem  
and then:  
curl -XPOST localhost:9200/filesystem/document/\_mapping -d '  
{  
"document": {  
"\_parent": { "type": "document"}  
}  
}'

Test data are:  
curl -XPOST localhost:9200/filesystem/document/1?parent=null -d '  
{  
"name": "root"  
}'

curl -XPOST localhost:9200/filesystem/document/2?parent=1 -d '  
{  
"name": "folder1"  
}'

curl -XPOST localhost:9200/filesystem/document/3?parent=2 -d '  
{  
"name": "file11"  
}'

curl -XPOST localhost:9200/filesystem/document/4?parent=2 -d '  
{  
"name": "file12"  
}'

curl -XPOST localhost:9200/filesystem/document/5?parent=1 -d '  
{  
"name": "folder2"  
}'

curl -XPOST localhost:9200/filesystem/document/6?parent=5 -d '  
{  
"name": "file21"  
}'

curl -XPOST localhost:9200/filesystem/document/7?parent=5 -d '  
{  
"name": "file22"  
}'

I wonder is it possible to get some document with all childs. For example I  
want to get root folder with all subfolders and files.  
If I simply try to search like this:  
curl -XPOST localhost:9200/filesystem/document/\_search -d '{  
"query": {  
"term": { "name": "root"}  
}  
}'

result is:  
{"took":3,"timed\_out":false,"\_shards":{"total":5,"successful":5,"failed":0},  
"hits":{"total":1,"max\_score":0.30685282,"hits":[{"\_index":"filesystem",  
"\_type":"document","\_id":"1","\_score":0.30685282, "\_source" :  
{  
"name": "root"  
}}]}}

But what I want is something like:  
...  
..., "\_source":  
{  
"name": "root",  
"child\_documents": [  
{  
"name": "folder1",  
"child\_documents": [  
{  
"name": "file11"  
},  
{  
"name": "file12"  
}  
]  
},  
{  
"name": "folder2",  
"child\_documents": [  
{  
"name": "file21"  
},  
{  
"name": "file22"  
}  
]  
}  
]  
}

Is it even possible in elasticsearch?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [July 6, 2017, 2:31am UTC](https://discuss.elastic.co/t/multiple-self-nesting/12399/2 "2017-07-06T02:31:24Z")

</div>


