# How to find all parents where child property exist

**URL:** <https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136>\
**Category:** Elasticsearch\
**Created:** [July 31, 2021, 7:16pm UTC](https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136 "2021-07-31T19:16:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANKIT\_BHARTI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankit_bharti/32/74548_2.png) [@ANKIT\_BHARTI](https://discuss.elastic.co/u/ANKIT_BHARTI)\
**Post date:** [July 31, 2021, 7:16pm UTC](https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136/1 "2021-07-31T19:16:01Z")

</div>

Hi All ,  
I am trying to solve a reverse query problem and reaching out to find the best possible way.  
I have an existing index which contains

```auto
A : [1,2,3,4]
B : [2,3]
C : [3,5,6] .....

```

10000 entries  
Parent is A , B ,C that contains child array of objects 1,2,3,4,5,6...  
I need to find which all parents where a child exist as list in a rest call to elastic.

```auto
1 : [A]
2: [A ,B]
3: [A,B,C]

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 2, 2021, 7:38am UTC](https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136/2 "2021-08-02T07:38:41Z")

</div>

Can you provide a full reproducible example including index creation and document creation. This way the mental model of everyone reading this will be much better and it will be easier to find a solution (are you using nested/ parent-child, which is the parent and the child etc is nothing we need to assume but know for a given!)

Thank you!

---

<div class="post-metadata">

**Author:** ![ANKIT\_BHARTI](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankit_bharti/32/74548_2.png) [@ANKIT\_BHARTI](https://discuss.elastic.co/u/ANKIT_BHARTI)\
**Post date:** [August 4, 2021, 9:31am UTC](https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136/3 "2021-08-04T09:31:54Z")

</div>

```auto
PUT /team

PUT team/_doc/A
{ "fans" : [{"id": 1} ,{"id":2 }, {"id" :3}] }

PUT team/_doc/B
{ "fans" : [{"id": 2} ,{"id": 3 }]}

PUT team/_doc/C
{ "fans" : [{"id": 3} ,{"id":4 }, {"id" :5 }] }

```

Need to find out , fan ID : 1 follows how many teams , similarly for fan ID 2,3,5,6  
Expected result :  
1 : [A], 2: [A,B] , 3 : [B,C]

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 4, 2021, 11:30am UTC](https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136/4 "2021-08-04T11:30:50Z")

</div>

you could change your data structure to `fans: ["1", "2", "3"]` by omitting the id.. now you can use a terms aggregation, or just run a query to search for `fans: 1` and count the number of results?

---

<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:** [September 1, 2021, 11:30am UTC](https://discuss.elastic.co/t/how-to-find-all-parents-where-child-property-exist/280136/5 "2021-09-01T11:30:53Z")

</div>

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