# Anyway to do a cyclic query? A query within a query

**URL:** <https://discuss.elastic.co/t/anyway-to-do-a-cyclic-query-a-query-within-a-query/49033>\
**Category:** Elasticsearch\
**Created:** [May 3, 2016, 9:49am UTC](https://discuss.elastic.co/t/anyway-to-do-a-cyclic-query-a-query-within-a-query/49033 "2016-05-03T09:49:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nithyanand\_Vasudevan](https://avatars.discourse-cdn.com/v4/letter/n/278dde/32.png) [@Nithyanand\_Vasudevan](https://discuss.elastic.co/u/Nithyanand_Vasudevan)\
**Post date:** [May 3, 2016, 9:49am UTC](https://discuss.elastic.co/t/anyway-to-do-a-cyclic-query-a-query-within-a-query/49033/1 "2016-05-03T09:49:55Z")

</div>

Hi,

Use case  
I have a set of documents, with two fields. In each document an edge is described from Field A to Field B. Thus, a complete Path is described from in document 1 : A(1) to B(2) and document 3 : A(2) to B(4) and document 2 : A(4) to B(6) and so on. (i.e) 1-2-2-4-4-6 and so on.

Possible Solution  
I was looking at tracing the relationships using nested aggregations. But once you come down A to B, you can't go to B to A because it will just display reversed results. So, if we can pass results of previous aggregation as a filter to B to A (i.e.) Pick A where A = B from previous result. I don't think there is any aggregation API for this.

Any advice will be welcome.

Thanks.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 3, 2016, 11:09pm UTC](https://discuss.elastic.co/t/anyway-to-do-a-cyclic-query-a-query-within-a-query/49033/2 "2016-05-03T23:09:34Z")

</div>

This is almost like a join, which you cannot do unfortunately.

---

<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 5, 2017, 10:54pm UTC](https://discuss.elastic.co/t/anyway-to-do-a-cyclic-query-a-query-within-a-query/49033/3 "2017-07-05T22:54:12Z")

</div>

Closed on July 5, 2017, 10:54pm UTC


