Using subaggregations considering only the first x matches

Hello, I am looking for a subaggregation which considers only the first x matches. I am working on a dataset which consists of thousands of cities, containing amongst others the two fields

  • cityName
  • cityDescription

The user can search in the field cityDescription to get the most suitable city. Therefore, in the first step, I aggregate the search results by cityName. The second subaggregation is about the score of the found documents. All works fine using the command:

GET cityIndex/_search
  "query": {
    "match": {
      "cityDescription": "Visit churches and have nice dinner in restaurants and go swimming in the sea"
  } , 
  "aggs": {
    "City": {
      "terms":  {
        "field": "cityName",
        "order": {
          "Score.sum": "desc"
      "aggs": {
        "Score": { 
          "stats": { 
            "script": "_score"

In the next step, I only want to consider and include the top ten documents for each city regarding the ranking of the cities.

How can that be done?

Thanks for your answers in advance, kind regards -


See the 'sampler' aggregation

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.