# Query field computed by child query?

**URL:** <https://discuss.elastic.co/t/query-field-computed-by-child-query/16677>\
**Category:** Elasticsearch\
**Created:** [March 28, 2014, 12:48pm UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677 "2014-03-28T12:48:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Andres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_andres/32/1685_2.png) [@Thomas\_Andres](https://discuss.elastic.co/u/Thomas_Andres)\
**Post date:** [March 28, 2014, 12:48pm UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677/1 "2014-03-28T12:48:52Z")

</div>

I have documents in a parent/child relation. In a query run on the parent,  
I'd like to know, if the found parents have children matching some query. I  
don't want to filter only parents with some conditions on the child, but  
only get the information, that they have childrens matching some query.

Any idea if that's possible? I've been thinking maybe adding a scrip\_field  
that would compute that, but have no idea how to run child queries from a  
script field.

An example to clarify my problem:  
child has a boolean field "error"

I run a query on the parent and want to show an information if any of the  
children has the error flag set.

Any hint would be welcome.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c5bbf2eb-7960-4740-9e0c-a70dbe98a9aa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c5bbf2eb-7960-4740-9e0c-a70dbe98a9aa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 28, 2014, 1:21pm UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677/2 "2014-03-28T13:21:30Z")

</div>

Not sure I understand. So if you run a \_search on the parent, and use the  
has\_child filter to return only parents that match some child condition, is  
that not what you want?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/d2eb0b34-bff4-4271-89a1-be7f60bcc54f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d2eb0b34-bff4-4271-89a1-be7f60bcc54f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Thomas\_Andres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_andres/32/1685_2.png) [@Thomas\_Andres](https://discuss.elastic.co/u/Thomas_Andres)\
**Post date:** [March 28, 2014, 1:28pm UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677/3 "2014-03-28T13:28:42Z")

</div>

I want to return "all" parents (or those matching some other query  
conditions) but in addition to the other data in the document, I want to  
compute for each parent, if he has any child with a set error flag. I don't  
want to filter on this condition in this case.

Am Freitag, 28. März 2014 14:21:30 UTC+1 schrieb Binh Ly:

> Not sure I understand. So if you run a \_search on the parent, and use the  
> has\_child filter to return only parents that match some child condition, is  
> that not what you want?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c99eb564-0767-4adc-b2f6-e4ca00c879a3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c99eb564-0767-4adc-b2f6-e4ca00c879a3%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 28, 2014, 8:04pm UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677/4 "2014-03-28T20:04:24Z")

</div>

I see, unfortunately I don't think you can do this at the moment. That  
being said, you might find this example interesting:

[https://github.com/imotov/elasticsearch-native-script-example/blob/master/example/lookup.sh](https://github.com/imotov/elasticsearch-native-script-example/blob/master/example/lookup.sh)

[https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java](https://github.com/imotov/elasticsearch-native-script-example/blob/master/src/main/java/org/elasticsearch/examples/nativescript/script/LookupScript.java)

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9ce38fdb-03df-4706-9e18-d8fa6778d6fc%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9ce38fdb-03df-4706-9e18-d8fa6778d6fc%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Thomas\_Andres](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_andres/32/1685_2.png) [@Thomas\_Andres](https://discuss.elastic.co/u/Thomas_Andres)\
**Post date:** [March 31, 2014, 7:13am UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677/5 "2014-03-31T07:13:02Z")

</div>

Thanks for the examples. Looks quite interesting. If I understand that  
correctly, I'd have to write a plugin doing my subquery. Too bad I don't  
have much time right now ☹ Sounds like an interesting challenge 🙂

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f76b7750-fcba-48dd-a1be-61d385b12bd4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f76b7750-fcba-48dd-a1be-61d385b12bd4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:39am UTC](https://discuss.elastic.co/t/query-field-computed-by-child-query/16677/6 "2017-07-06T01:39:33Z")

</div>


