# Tree structure query on flat objects

**URL:** <https://discuss.elastic.co/t/tree-structure-query-on-flat-objects/138418>\
**Category:** Elasticsearch\
**Created:** [July 3, 2018, 3:43pm UTC](https://discuss.elastic.co/t/tree-structure-query-on-flat-objects/138418 "2018-07-03T15:43:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![magician](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@magician](https://discuss.elastic.co/u/magician)\
**Post date:** [July 3, 2018, 3:43pm UTC](https://discuss.elastic.co/t/tree-structure-query-on-flat-objects/138418/1 "2018-07-03T15:43:05Z")

</div>

I have the following data model (just an example) stored in one index:

- object { id: 1, parent: null, title: ....}
- object { id: 2, parent: 1, title: ....}
- object { id: 3, parent: 1, title: ....}
- ...

Now, suppose I want to do a query listing all the objects having parent= null (root objects) and for each of these root objects I want the children object as children in the json response, e.g.

object {  
id: 1, parent: null, title: ... ,  
childrens [  
object { id: 2, parent: 1, title: ....},  
object { id: 3, parent: 1, title: ....}  
]

Is it possible?

I simplified a lot the use case, but I have a lot of attributes for each object and most of the time I do query on single object only. Only for this use case I show, I would not like to replicate the tree structure in a separate index, because I should duplicate a lot of data.

Thanks!

---

<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 31, 2018, 3:45pm UTC](https://discuss.elastic.co/t/tree-structure-query-on-flat-objects/138418/2 "2018-07-31T15:45:09Z")

</div>

Closed on July 31, 2018, 3:45pm UTC

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