Hello Friends,
Request
Searching for 2 keys and displaying the aggregations alongside.
GET /new_irds_ld_tf/_search
{
  "size": 1,
  "query": {
    "bool": {
      "must": {
        "term": {
          "status.keyword": "active"
        }
      },
      "filter": {
        "terms": {
          "AltID.ISIN.keyword": [
            "DE000A0Z3CW9",
            "KR4157M60003"
          ]
        }
      }
    }
  },
  "sort": [
    {
      "AltID.ISIN.keyword": {
        "order": "asc"
      }
    },
    "_score"
  ],
  "aggs": {
    "AltID": {
      "terms": {
        "field": "AltID.ISIN.keyword"
      }
    }
  }
}
Response
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 17,
    "max_score": null,
    "hits": [
      {
        "_index": "new_irds_ld_tf",
        "_id": "ld_tf_145066457",
        "_score": 0.50210315,
        "_source": {
          "AltID": {
            "ISIN": "DE000A0Z3CW9",
          },
          "RelAltID": {
            "rel_RIC": ".V2TX"
          },
          "status": "active"
        },
        "sort": [
          "DE000A0Z3CW9",
          0.50210315
        ]
      }
    ]
  },
  "aggregations": {
    "AltID": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "DE000A0Z3CW9",
          "doc_count": 16
        },
        {
          "key": "KR4157M60003",
          "doc_count": 1
        }
      ]
    }
  }
}
As per the response above, there are 16 docs matching "key": "DE000A0Z3CW9" and 1 doc matching "key": "KR4157M60003".
Here I would like the response to contain the first top-score document for each of these keys (i.e. one doc with the highest score for key DE000A0Z3CW9 and one doc with the highest score for key KR4157M60003).
Please advice.
 Thank you so much.