Elasticsearch documentation states that The top_hits aggregation returns regular search hits, because of this many per hit features can be supported
Crucially, the list includes _Named filters and queries_ But trying to add any filter or query throws SearchParseException: Unknown key for a START_OBJECT. So, it looks like the documentation is wrong.
Stack overflow question: http://stackoverflow.com/questions/34319337/filter-query-support-in-elasticsearch-top-hits-aggregation
Indeed the TopHitsParser does not seem able to parse query/filters via the SubSearchContext, but maybe I am misreading the source. Maybe @martijnvg can clarify here.
Thinking if you can try a filter aggregation and nest the top_hits inside of that one?
@sumitjainn This is for filters and queries that have the _name option set in the main query. So hits returned by the top_hits agg can included reference to named queries/filters used in the main query. It isn't possible to add a query/filter to the top_hits agg.
Thanks for clearing it up @martijnvg. @spinscale Yes, indeed, filter aggregations does the trick, was just curious about the correctness of the documentation.
thx for responding. closing then!
@sumitjainn can you post an example here? trying to do some filtering on the docs of a top_hits aggregation, but didnt have any luck here... maybe @spinscale has an opinion too?
I'm also unable to do this. @sumitjainn or @spinscale , do you have an example please?
@digitalkaoz @dukenguyen - i think they meant to wrap it with filter aggregation.
This is what i did:
"aggregations": {
"last_document_of_specific_doc_types": {
"filter": {
"terms": {
"doc_types": [161, 162]
}
},
"aggregations": {
"latest": {
"top_hits": {
"size": 1,
"_source": [
"creationDate"
],
"sort": {
"creationDate": "desc"
}
}
}
}
}
}
@itaydvir
If I want to access a field from the result returned by top_hits:(example below)
"aggregations": {
"last_document_of_specific_doc_types": {
"filter": {
"terms": {
"doc_types": [
161,
162
]
}
},
"aggregations": {
"latest": {
"top_hits": {
"size": 1,
"_source": [
"creationDate",
"required"
],
"sort": {
"creationDate": "desc"
},
"script_fields": {
"reqd_flag": {
"script": {
"lang": "painless",
"inline": "params['_source']['required'] == 'Y' ? 0 : 1"
}
}
}
}
}
}
}
}
How can I access the source field "required" or the scripted field "reqd_flag" outside this aggregation ? If not, then can you suggest an alternative to this. Am I missing or doing something wrong ?
Most helpful comment
I'm also unable to do this. @sumitjainn or @spinscale , do you have an example please?