Skip to content
Snippets Groups Projects
Commit 6fbe00dc authored by Aivaras Gotovskis's avatar Aivaras Gotovskis
Browse files

Add top hits aggregation doc.

parent 51895ced
No related branches found
No related tags found
No related merge requests found
# Top Hits Aggregation
> More info about top hits aggregation is in the [official elasticsearch docs][1]
A top hits metric aggregator keeps track of the most relevant document
being aggregated. This aggregator is intended to be used as a sub aggregator,
so that the top matching documents can be aggregated per bucket.
## Simple example
```JSON
{
"aggregation": {
"top-tags": {
"terms": {
"field": "tags",
"size": 3
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"sort": [
{
"last_activity_date": {
"order": "desc"
}
}
],
"_source": {
"include": [
"title"
]
},
"size" : 1
}
}
}
}
}
}
```
And now the query via DSL:
```php
$sort = new Sort('last_activity_date', Sort::ORDER_DESC);
$topHitsAggregation = new TopHitsAggregation('top_tag_hits', 1, null, $sort);
$topHitsAggregation->addParameter('_source', ['include' => ['title']]);
$termsAggregation = new TermsAggregation('top-tags', 'tags');
$termsAggregation->addParameter('size', 3);
$termsAggregation->addAggregation($topHitsAggregation);
$search = new Search();
$search->addAggregation($termsAggregation);
$queryArray = $search->toArray();
```
[1]: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment