# Term Aggregation and displaying top scored document for each term

**URL:** <https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905>\
**Category:** Elasticsearch\
**Created:** [April 20, 2018, 3:24pm UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905 "2018-04-20T15:24:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [April 20, 2018, 3:24pm UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/1 "2018-04-20T15:24:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [April 27, 2018, 9:35am UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/2 "2018-04-27T09:35:32Z")

</div>

> [@LGans316](#):
>
> the first top-score document for each of these keys

Use a [top\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) aggregation with the size set to 1

---

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [April 27, 2018, 5:45pm UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/3 "2018-04-27T17:45:45Z")

</div>

Thanks Mark but due to some requirement changes, the request query has become like this:

**Request**

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

```

**Response**

```
{
  "aggregations": {
    "AltID": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "N.A.",
          "doc_count": 147,
          "IndexName": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "new_irds_ld_to",
                "doc_count": 105
              },
              {
                "key": "new_irds_ld_tf",
                "doc_count": 42
              }
            ]
          }
        }
      ]
    }
  }
}

```

In each bucket, I would like top scored doc to be displayed which I am unable to figure out.

---

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [April 30, 2018, 8:51am UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/4 "2018-04-30T08:51:12Z")

</div>

Any help would be appreciated as I am unable to figure out how to retrieve the top scored doc grouping by fieldname, indexName.

---

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [May 1, 2018, 10:14am UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/5 "2018-05-01T10:14:26Z")

</div>

Still searching for answers ....

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [May 1, 2018, 10:53am UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/6 "2018-05-01T10:53:42Z")

</div>

You nest the `top_hits` aggregation under the `IndexName` agg exactly the same way that `IndexName` is nested under the containing`AltID` aggregation. It's just layers of nested JSON to represent the tree of aggregations you want.

---

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [May 1, 2018, 12:00pm UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/7 "2018-05-01T12:00:25Z")

</div>

Wow. Thanks a lot @Mark_Harwood . Looking good now and looks like I simply wasn't paying attention earlier. 😊 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
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 29, 2018, 12:00pm UTC](https://discuss.elastic.co/t/term-aggregation-and-displaying-top-scored-document-for-each-term/128905/8 "2018-05-29T12:00:26Z")

</div>

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