From 6fbe00dcd879519c7bf867d2ecda522c204da25a Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io> Date: Fri, 10 Jul 2015 12:07:52 +0300 Subject: [PATCH] Add top hits aggregation doc. --- docs/Aggregation/TopHits.md | 60 +++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 docs/Aggregation/TopHits.md diff --git a/docs/Aggregation/TopHits.md b/docs/Aggregation/TopHits.md new file mode 100644 index 0000000..2b3d08f --- /dev/null +++ b/docs/Aggregation/TopHits.md @@ -0,0 +1,60 @@ +# 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 -- GitLab