Term Aggregation and displaying top scored document for each term

Wow. Thanks a lot @Mark_Harwood . Looking good now and looks like I simply wasn't paying attention earlier. :blush: Thank you so much.

Working Request

GET /new_irds_*/_search?filter_path=aggregations
{
  "size": 0,
  "query": {
    "bool": {
      "must": {
        "term": {
          "status.keyword": "active"
        }
      },
      "filter": {
        "terms": {
          "AltID.ISIN.keyword": [
            "N.A.",
            "DE000A0G9C70"
          ]
        }
      }
    }
  },
  "aggs": {
    "AltID": {
      "terms": {
        "field": "AltID.ISIN.keyword"
      },
      "aggs": {
        "IndexName": {
          "terms": {
            "field": "_index"
          },
          "aggs": {
            "top_hits": {
              "top_hits": {
                "sort": [
                  {
                    "_score": {
                      "order": "desc"
                    }
                  }
                ],
                "size": 1
              }
            }
          }
        }
      }
    }
  }
}
1 Like