Using top_hits to get the best matching nested doc inside another query


(NickV) #1

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:

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/35ac95ca-fa44-4b24-914d-059df002ae5e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #2