# Can graph query (non-cyclic, non-duplicated) ancestors and descendants?

**URL:** <https://discuss.elastic.co/t/can-graph-query-non-cyclic-non-duplicated-ancestors-and-descendants/49339>\
**Category:** Kibana\
**Tags:** elastic-stack-graph\
**Created:** [May 5, 2016, 8:08pm UTC](https://discuss.elastic.co/t/can-graph-query-non-cyclic-non-duplicated-ancestors-and-descendants/49339 "2016-05-05T20:08:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![will.archive](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/will.archive/32/9616_2.png) [@will.archive](https://discuss.elastic.co/u/will.archive)\
**Post date:** [May 5, 2016, 8:08pm UTC](https://discuss.elastic.co/t/can-graph-query-non-cyclic-non-duplicated-ancestors-and-descendants/49339/1 "2016-05-05T20:08:23Z")

</div>

So assume we have documents have a field `children`, which can contain the ids of other documents. For example:

```auto
{_id: 1 :children [2, 3, 4]}
{_id: 2 :children [5, 6]}
{_id: 3}
{_id: 4}
{_id: 5 :children [6, 7, 8]}
{_id: 6}
{_id: 7}
{_id: 8}

```

Is there a graph query that will gather all the ancestors of `8`? (i.e., `[1, 2, 5]`) All the descendants of `2` (i.e, `[5, 6, 7, 8]`)? The ancestors of `5` (i.e., `[1,2]`)? The descendants of `5` (i.e., `[6, 7, 8]`).

This, of course, in one query call.

Can the ancestors query be done efficiently (with, perhaps, a total depth of 10)?

Can the descendants query be done efficiently (with, perhaps, thousands or tens of thousands of descendants)?

---

<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:** [May 6, 2016, 9:00am UTC](https://discuss.elastic.co/t/can-graph-query-non-cyclic-non-duplicated-ancestors-and-descendants/49339/2 "2016-05-06T09:00:27Z")

</div>

Short answer is "no" plus here is the longer answer:

> Is there a graph query that will gather all the ancestors...in one query call

Not with that data structure. The challenge is you have to take a value from one field, "\_id" and use it in a query to another, "children" then vice-versa. This would require you to tell us about these fields where values from one can be used in another to map out the link structure. That's a level of complexity we've chosen not to add at this stage.

I did consider an alternative data model with a "MeAndMyParent" field with two values e.g. "MeAndMyParent": [6,5] but another implicit part of an ancestor crawl is that you don't go up to a parent ID then follow down to brothers and cousins etc. The general-purpose graph exploration logic doesn't have any in-built support for enforcing the special-case rules that apply to following up tree structures.

Cheers  
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:** [July 6, 2017, 1:43pm UTC](https://discuss.elastic.co/t/can-graph-query-non-cyclic-non-duplicated-ancestors-and-descendants/49339/3 "2017-07-06T13:43:36Z")

</div>


