# Scoring a parent document search by a count of children matching part of the query?

**URL:** <https://discuss.elastic.co/t/scoring-a-parent-document-search-by-a-count-of-children-matching-part-of-the-query/17435>\
**Category:** Elasticsearch\
**Created:** [May 9, 2014, 4:10pm UTC](https://discuss.elastic.co/t/scoring-a-parent-document-search-by-a-count-of-children-matching-part-of-the-query/17435 "2014-05-09T16:10:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbrianjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbrianjones/32/1402_2.png) [@tbrianjones](https://discuss.elastic.co/u/tbrianjones)\
**Post date:** [May 9, 2014, 4:10pm UTC](https://discuss.elastic.co/t/scoring-a-parent-document-search-by-a-count-of-children-matching-part-of-the-query/17435/1 "2014-05-09T16:10:19Z")

</div>

I have an index with parent documents ( Companies ), that have children (  
Files ). Each Company can have hundreds of Files. Companies and Files  
both have many fields.

The search I'm trying to perform is the Company that best matches based on  
it's own fields and the fields of it's children ( the Files ). The current  
query I run is a Bool-Should query where I perform a has\_child query on the  
files and a regular query on the Companies. I only require a minimum of  
one match so, as I understand it, a Company that matches it's own fields  
and one of it's children will score higher than a Company that only  
matchesit's own fields. You'll see I also have to apply a nuber of filters  
to the Companies.

I'm wondering if there is a way to query the system where it will take all  
the children into account, and not just one. If ten Files match the query,  
then that Company result would likely score higher than a Company that only  
had a few files match ... obviously there would be other scoring going on  
... so maybe some sort of multiplyer applied to the sum of children scores  
would be appropriate. It's defining a query that matches multiple children  
that I'm unable to figure out.

Here is an example of the query that I currently use:

{  
"query": {  
"filtered": {  
"filter": {  
"and": [  
{  
"terms": {  
"\_cache": true,  
"execution": "or",  
"locations.state": [  
"california",  
"maryland"  
]  
}  
},  
{  
"terms": {  
"\_cache": true,  
"execution": "and",  
"industries.term.not\_analyzed": [  
"aerospace",  
"defense"  
]  
}  
},  
{  
"geo\_distance": {  
"locations.geolocation": {  
"lat": "41",  
"lon": "-82"  
},  
"distance": "25mi"  
}  
}  
]  
},  
"query": {  
"bool": {  
"should": [  
{  
"query\_string": {  
"default\_field": "\_all",  
"query": "adhesive"  
}  
},  
{  
"has\_child": {  
"type": "file",  
"query": {  
"query\_string": {  
"default\_field": "\_all",  
"query": "adhesive"  
}  
}  
}  
}  
],  
"minimum\_number\_should\_match": 1  
}  
}  
}  
}  
}

--  
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/261146ca-c994-40d4-a970-6b5d872bb13e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/261146ca-c994-40d4-a970-6b5d872bb13e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![tbrianjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbrianjones/32/1402_2.png) [@tbrianjones](https://discuss.elastic.co/u/tbrianjones)\
**Post date:** [May 9, 2014, 4:54pm UTC](https://discuss.elastic.co/t/scoring-a-parent-document-search-by-a-count-of-children-matching-part-of-the-query/17435/2 "2014-05-09T16:54:40Z")

</div>

It seems like nesting the Files within the Company docs may be the only  
solution here. That is definitely an option. I had indexed the Files as  
children of Companies so that I could query the Files as a separate index (  
which I also need to do ), but can maintain a separate index alltogether if  
need be.

On Friday, May 9, 2014 9:10:19 AM UTC-7, Brian Jones wrote:

> I have an index with parent documents ( Companies ), that have children (  
> Files ). Each Company can have hundreds of Files. Companies and Files  
> both have many fields.
> 
> The search I'm trying to perform is the Company that best matches based on  
> it's own fields and the fields of it's children ( the Files ). The current  
> query I run is a Bool-Should query where I perform a has\_child query on the  
> files and a regular query on the Companies. I only require a minimum of  
> one match so, as I understand it, a Company that matches it's own fields  
> and one of it's children will score higher than a Company that only  
> matchesit's own fields. You'll see I also have to apply a nuber of filters  
> to the Companies.
> 
> I'm wondering if there is a way to query the system where it will take all  
> the children into account, and not just one. If ten Files match the query,  
> then that Company result would likely score higher than a Company that only  
> had a few files match ... obviously there would be other scoring going on  
> ... so maybe some sort of multiplyer applied to the sum of children scores  
> would be appropriate. It's defining a query that matches multiple children  
> that I'm unable to figure out.
> 
> Here is an example of the query that I currently use:
> 
> {  
> "query": {  
> "filtered": {  
> "filter": {  
> "and": [  
> {  
> "terms": {  
> "\_cache": true,  
> "execution": "or",  
> "locations.state": [  
> "california",  
> "maryland"  
> ]  
> }  
> },  
> {  
> "terms": {  
> "\_cache": true,  
> "execution": "and",  
> "industries.term.not\_analyzed": [  
> "aerospace",  
> "defense"  
> ]  
> }  
> },  
> {  
> "geo\_distance": {  
> "locations.geolocation": {  
> "lat": "41",  
> "lon": "-82"  
> },  
> "distance": "25mi"  
> }  
> }  
> ]  
> },  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "query\_string": {  
> "default\_field": "\_all",  
> "query": "adhesive"  
> }  
> },  
> {  
> "has\_child": {  
> "type": "file",  
> "query": {  
> "query\_string": {  
> "default\_field": "\_all",  
> "query": "adhesive"  
> }  
> }  
> }  
> }  
> ],  
> "minimum\_number\_should\_match": 1  
> }  
> }  
> }  
> }  
> }

--  
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/d19b8cda-a4e3-415a-9920-ca48888ec0b4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d19b8cda-a4e3-415a-9920-ca48888ec0b4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![tbrianjones](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbrianjones/32/1402_2.png) [@tbrianjones](https://discuss.elastic.co/u/tbrianjones)\
**Post date:** [May 9, 2014, 4:57pm UTC](https://discuss.elastic.co/t/scoring-a-parent-document-search-by-a-count-of-children-matching-part-of-the-query/17435/3 "2014-05-09T16:57:06Z")

</div>

Are there any gotchas I should be aware of when creating a document that  
could contain thousands of pages of text ( a Company and thousands of  
nested Files ) in addition to dozens/hundreds of fields?

On Friday, May 9, 2014 9:54:40 AM UTC-7, Brian Jones wrote:

> It seems like nesting the Files within the Company docs may be the only  
> solution here. That is definitely an option. I had indexed the Files as  
> children of Companies so that I could query the Files as a separate index (  
> which I also need to do ), but can maintain a separate index alltogether if  
> need be.
> 
> On Friday, May 9, 2014 9:10:19 AM UTC-7, Brian Jones wrote:
> 
> > I have an index with parent documents ( Companies ), that have children (  
> > Files ). Each Company can have hundreds of Files. Companies and Files  
> > both have many fields.
> > 
> > The search I'm trying to perform is the Company that best matches based  
> > on it's own fields and the fields of it's children ( the Files ). The  
> > current query I run is a Bool-Should query where I perform a has\_child  
> > query on the files and a regular query on the Companies. I only require a  
> > minimum of one match so, as I understand it, a Company that matches it's  
> > own fields and one of it's children will score higher than a Company that  
> > only matchesit's own fields. You'll see I also have to apply a nuber of  
> > filters to the Companies.
> > 
> > I'm wondering if there is a way to query the system where it will take  
> > all the children into account, and not just one. If ten Files match the  
> > query, then that Company result would likely score higher than a Company  
> > that only had a few files match ... obviously there would be other scoring  
> > going on ... so maybe some sort of multiplyer applied to the sum of  
> > children scores would be appropriate. It's defining a query that matches  
> > multiple children that I'm unable to figure out.
> > 
> > Here is an example of the query that I currently use:
> > 
> > {  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "and": [  
> > {  
> > "terms": {  
> > "\_cache": true,  
> > "execution": "or",  
> > "locations.state": [  
> > "california",  
> > "maryland"  
> > ]  
> > }  
> > },  
> > {  
> > "terms": {  
> > "\_cache": true,  
> > "execution": "and",  
> > "industries.term.not\_analyzed": [  
> > "aerospace",  
> > "defense"  
> > ]  
> > }  
> > },  
> > {  
> > "geo\_distance": {  
> > "locations.geolocation": {  
> > "lat": "41",  
> > "lon": "-82"  
> > },  
> > "distance": "25mi"  
> > }  
> > }  
> > ]  
> > },  
> > "query": {  
> > "bool": {  
> > "should": [  
> > {  
> > "query\_string": {  
> > "default\_field": "\_all",  
> > "query": "adhesive"  
> > }  
> > },  
> > {  
> > "has\_child": {  
> > "type": "file",  
> > "query": {  
> > "query\_string": {  
> > "default\_field": "\_all",  
> > "query": "adhesive"  
> > }  
> > }  
> > }  
> > }  
> > ],  
> > "minimum\_number\_should\_match": 1  
> > }  
> > }  
> > }  
> > }  
> > }

--  
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/44ac19ce-7571-41e4-a607-5e6b2fbdd1d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/44ac19ce-7571-41e4-a607-5e6b2fbdd1d4%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:30am UTC](https://discuss.elastic.co/t/scoring-a-parent-document-search-by-a-count-of-children-matching-part-of-the-query/17435/4 "2017-07-06T01:30:29Z")

</div>


