# Using top\_hits to get the best matching nested doc inside another query

**URL:** <https://discuss.elastic.co/t/using-top-hits-to-get-the-best-matching-nested-doc-inside-another-query/23474>\
**Category:** Elasticsearch\
**Created:** [April 29, 2015, 4:05pm UTC](https://discuss.elastic.co/t/using-top-hits-to-get-the-best-matching-nested-doc-inside-another-query/23474 "2015-04-29T16:05:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickV](https://avatars.discourse-cdn.com/v4/letter/n/a8b319/32.png) [@NickV](https://discuss.elastic.co/u/NickV)\
**Post date:** [April 29, 2015, 4:05pm UTC](https://discuss.elastic.co/t/using-top-hits-to-get-the-best-matching-nested-doc-inside-another-query/23474/1 "2015-04-29T16:05:05Z")

</div>

Hi all,

I'm wondering whether it's possible to do a search, and then score the  
nested docs and use the top\_hits aggregator to pick the best one.

I've come up with an example to illustrate this:

> <https://gist.github.com/npvincent/aecc17a81515f56eadbf>

This is a toy example of the problem (I'm doing something more advanced to  
find the author) , but essentially if my documents look like this  
{  
"name": "tolkien",  
"books": [  
{"title":"Return of the King", "pages":431},  
{"title":"The Fellowship of the Ring", "pages":390},  
{"title":"The Two Towers", "pages":376},  
{"title":"The Hobbit", "pages":192 }  
]  
}

and my mappings look like this

{  
"properties": {  
"name": {  
"type": "string"  
},  
"books": {  
"type": "nested",  
"properties": {  
"title": {  
"type": "string"  
},  
"pages": {  
"type": "integer"  
}  
}  
}  
}  
}

I would like to be able to do a search for relevant authors, and return the  
book that best matches the search criteria (in this example the number of  
pages in it). I've been looking extensively but I can't find any  
information on whether the nested documents can have their own relevance  
scores. Nothing I can seem to do to this query effects which book ends up  
in the "top\_book" aggregation, and for this I would expect it to be The Two  
Towers as it is a better match, but it's always The Hobbit. Am I just  
trying to do something impossible?

{  
"query": {  
"bool": {  
"must": [  
{"term": {"name":"tolkien"}}],  
"should": [  
{"nested": {  
"path": "books",  
"query": {  
"term": {"pages": 376}  
}  
}}  
]  
}  
},  
"aggs": {  
"results": {  
"terms": { "field": "name" },  
"aggs": {  
"books": {  
"nested": {  
"path": "books"  
},  
"aggs": {  
"top\_book": { "top\_hits": { "size": 1 } }  
}  
}  
}  
}  
}  
}

Cheers,

Nick

--  
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/35ac95ca-fa44-4b24-914d-059df002ae5e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/35ac95ca-fa44-4b24-914d-059df002ae5e%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, 12:16am UTC](https://discuss.elastic.co/t/using-top-hits-to-get-the-best-matching-nested-doc-inside-another-query/23474/2 "2017-07-06T00:16:58Z")

</div>


