# Querying both parent as child document fields

**URL:** <https://discuss.elastic.co/t/querying-both-parent-as-child-document-fields/12019>\
**Category:** Elasticsearch\
**Created:** [May 18, 2013, 8:39am UTC](https://discuss.elastic.co/t/querying-both-parent-as-child-document-fields/12019 "2013-05-18T08:39:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_Gaeremynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_gaeremynck/32/2142_2.png) [@Simon\_Gaeremynck](https://discuss.elastic.co/u/Simon_Gaeremynck)\
**Post date:** [May 18, 2013, 8:39am UTC](https://discuss.elastic.co/t/querying-both-parent-as-child-document-fields/12019/1 "2013-05-18T08:39:29Z")

</div>

Hello,

I'm struggling a bit to write a query that would query fields on a parent  
document and the fields on a child document.

first thought, my mapping:  
I have documents of type 'resource' (these are the parent docs)  
which have a couple of fields, the ones I'll be querying are called  
'q\_high' and 'q\_low'.  
I also have documents of type 'message' (the child documents) which have  
the same q\_high and q\_low fields  
We've modelled it this way as all top level resources can have messages  
associated with them.  
When we search for resources, we'd also like to search trough all the  
messages that are associated with a resource.

Now I want to write a query where:

- the q\_high or q\_low field match a string (chicken in the example below)
- OR the q\_high/q\_low field match the same string.

When a child document matches, I only need the parent document.

So far, I have the following Bool query:

{  
"bool": {  
"should": [  
{  
"multi\_match": {  
"fields": [  
"q\_high^2.0",  
"q\_low^0.75"  
],  
"query": "chicken"  
}  
},  
{  
"has\_child": {  
"type": "message",  
"query": {  
"multi\_match": {  
"fields": [  
"q\_high^2.0",  
"q\_low^0.75"  
],  
"query": "chicken"  
}  
}  
}  
}  
]  
}  
}

This works, but the more content I put into the system the lower the score  
of child documents get.  
When there are only 2 documents in the system (the parent and child) the  
child gets a score of about 0.6.  
When there are about 660 documents in the system, the score plumets to 0.02

Is there a better way to do queries like this?

Kind regards,

Simon Gaeremynck

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 18, 2013, 11:47am UTC](https://discuss.elastic.co/t/querying-both-parent-as-child-document-fields/12019/2 "2013-05-18T11:47:27Z")

</div>

HI Simon

Why do you care about the absolute score? It's not important. The only  
thing that is important is the relative difference between each doc, ie  
that more relevant docs appear higher than less relevant.

Note that has\_child defaults to not scoring matching children. You have to  
explicitly set the `score_mode` (called `score_type` in the current  
versions of ES, but has been fixed in master to be consistent) to eg `sum``

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

On 18 May 2013 10:39, Simon Gaeremynck [gaeremyncks@gmail.com](mailto:gaeremyncks@gmail.com) wrote:

> Hello,
> 
> I'm struggling a bit to write a query that would query fields on a parent  
> document and the fields on a child document.
> 
> first thought, my mapping:  
> I have documents of type 'resource' (these are the parent docs)  
> which have a couple of fields, the ones I'll be querying are called  
> 'q\_high' and 'q\_low'.  
> I also have documents of type 'message' (the child documents) which have  
> the same q\_high and q\_low fields  
> We've modelled it this way as all top level resources can have messages  
> associated with them.  
> When we search for resources, we'd also like to search trough all the  
> messages that are associated with a resource.
> 
> Now I want to write a query where:
> 
> - the q\_high or q\_low field match a string (chicken in the example below)
> - OR the q\_high/q\_low field match the same string.
> 
> When a child document matches, I only need the parent document.
> 
> So far, I have the following Bool query:
> 
> {  
> "bool": {  
> "should": [  
> {  
> "multi\_match": {  
> "fields": [  
> "q\_high^2.0",  
> "q\_low^0.75"  
> ],  
> "query": "chicken"  
> }  
> },  
> {  
> "has\_child": {  
> "type": "message",  
> "query": {  
> "multi\_match": {  
> "fields": [  
> "q\_high^2.0",  
> "q\_low^0.75"  
> ],  
> "query": "chicken"  
> }  
> }  
> }  
> }  
> ]  
> }  
> }
> 
> This works, but the more content I put into the system the lower the score  
> of child documents get.  
> When there are only 2 documents in the system (the parent and child) the  
> child gets a score of about 0.6.  
> When there are about 660 documents in the system, the score plumets to 0.02
> 
> Is there a better way to do queries like this?
> 
> Kind regards,
> 
> Simon Gaeremynck
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Simon\_Gaeremynck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simon_gaeremynck/32/2142_2.png) [@Simon\_Gaeremynck](https://discuss.elastic.co/u/Simon_Gaeremynck)\
**Post date:** [May 18, 2013, 2:38pm UTC](https://discuss.elastic.co/t/querying-both-parent-as-child-document-fields/12019/3 "2013-05-18T14:38:44Z")

</div>

Hi Clint,

Setting the score\_mode fixed it.  
In hindsight it's unbelievable that I missed that bit of information in the documentation as it's right there.  
Thank you for the patient reply.

Regards,

Simon

On 18 May 2013, at 12:47, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> HI Simon
> 
> Why do you care about the absolute score? It's not important. The only thing that is important is the relative difference between each doc, ie that more relevant docs appear higher than less relevant.
> 
> Note that has\_child defaults to not scoring matching children. You have to explicitly set the `score_mode` (called `score_type` in the current versions of ES, but has been fixed in master to be consistent) to eg `sum``
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/has-child-query/)
> 
> clint
> 
> On 18 May 2013 10:39, Simon Gaeremynck [gaeremyncks@gmail.com](mailto:gaeremyncks@gmail.com) wrote:  
> Hello,
> 
> I'm struggling a bit to write a query that would query fields on a parent document and the fields on a child document.
> 
> first thought, my mapping:  
> I have documents of type 'resource' (these are the parent docs)  
> which have a couple of fields, the ones I'll be querying are called 'q\_high' and 'q\_low'.  
> I also have documents of type 'message' (the child documents) which have the same q\_high and q\_low fields  
> We've modelled it this way as all top level resources can have messages associated with them.  
> When we search for resources, we'd also like to search trough all the messages that are associated with a resource.
> 
> Now I want to write a query where:
> 
> - the q\_high or q\_low field match a string (chicken in the example below)
> - OR the q\_high/q\_low field match the same string.
> 
> When a child document matches, I only need the parent document.
> 
> So far, I have the following Bool query:
> 
> {  
> "bool": {  
> "should": [  
> {  
> "multi\_match": {  
> "fields": [  
> "q\_high^2.0",  
> "q\_low^0.75"  
> ],  
> "query": "chicken"  
> }  
> },  
> {  
> "has\_child": {  
> "type": "message",  
> "query": {  
> "multi\_match": {  
> "fields": [  
> "q\_high^2.0",  
> "q\_low^0.75"  
> ],  
> "query": "chicken"  
> }  
> }  
> }  
> }  
> ]  
> }  
> }
> 
> This works, but the more content I put into the system the lower the score of child documents get.  
> When there are only 2 documents in the system (the parent and child) the child gets a score of about 0.6.  
> When there are about 660 documents in the system, the score plumets to 0.02
> 
> Is there a better way to do queries like this?
> 
> Kind regards,
> 
> Simon Gaeremynck
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/qhB9PLdON7Q/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/qhB9PLdON7Q/unsubscribe?hl=en-US).  
> To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:35am UTC](https://discuss.elastic.co/t/querying-both-parent-as-child-document-fields/12019/4 "2017-07-06T02:35:52Z")

</div>


