# Can I access the field values in top hits aggs?

**URL:** <https://discuss.elastic.co/t/can-i-access-the-field-values-in-top-hits-aggs/202983>\
**Category:** Elasticsearch\
**Created:** [October 10, 2019, 8:59am UTC](https://discuss.elastic.co/t/can-i-access-the-field-values-in-top-hits-aggs/202983 "2019-10-10T08:59:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yungyoung\_Ok](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yungyoung_ok/32/43465_2.png) [@Yungyoung\_Ok](https://discuss.elastic.co/u/Yungyoung_Ok)\
**Post date:** [October 10, 2019, 8:59am UTC](https://discuss.elastic.co/t/can-i-access-the-field-values-in-top-hits-aggs/202983/1 "2019-10-10T08:59:45Z")

</div>

my data....  
{"first\_seq":"aa", "second\_seq":1, "status":"LOADING"}  
{"first\_seq":"aa", "second\_seq":2, "status":"LOADING"}  
{"first\_seq":"aa", "second\_seq":3, "status":"LOADING"}  
{"first\_seq":"aa", "second\_seq":4, "status":"OK"}  
{"first\_seq":"bb", "second\_seq":10, "status":"LOADING"}  
{"first\_seq":"bb", "second\_seq":20, "status":"LOADING"}  
{"first\_seq":"bb", "second\_seq":30, "status":"LOADING"}  
{"first\_seq":"bb", "second\_seq":40, "status":"ERROR"}

**I only want to get {"first\_seq":"bb", "second\_seq":40, "status":"ERROR"}.**  
**count:1**

* * *

**First, I want to access the status field of the document where second\_seq is last**

```
GET my_index*/_search?size=0
{
  "aggs": {
    "first_seq_terms": {
      "terms": {
        "field": "first_seq.keyword",
        "size": 100
      },
      "aggs": {
        "top_hits_second_seq": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "second_seq": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": "status"
            }
          }
        }
      }
    }
  }
}

```

How can i access top\_hits\_second\_seq\>status????

* * *

**Second**  
**If I can't access the field values ​​in top hits aggs, How can i get buckets count of terms aggs?**

I couldn't find a way to access top hits internal field, so I made another aggs.

```
GET my_index*/_search?size=0
{
  "aggs": {
    "first_seq_terms": {
      "terms": {
        "field": "first_seq.keyword",
        "size": 10000
      },
      "aggs": {
        "top_hits_snd_seq": {
          "top_hits": {
            "size": 1,
            "sort": [
              {
                "second_seq": {
                  "order": "desc"
                }
              }
            ],
            "_source": {
              "includes": "status"
            }
          }
        },
        "sts_filter": {
          "filter": {
            "bool": {
              "must": [
                {
                  "match": {
                    "status.keyword": "OK"
                  }
                }
              ]
            }
          }
        },
          "status_err_count": {
            "bucket_selector": {
              "buckets_path": {
                "sts_ok_diff": "sts_filter>_count",
                "uniq_first_seq_doc_count": "_count"
              },
              "script": "params.proc_sts_ok_diff == 0"
            }
          }
      }
    }
  }
}

```

How can i get buckets count of first\_seq\_terms???

And What is maximum bucket count of terms aggregation???  
Even if it has hundreds of thousands of buckets, can i get them all?

**Or is there a better way than this?**

---

<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:** [November 7, 2019, 8:59am UTC](https://discuss.elastic.co/t/can-i-access-the-field-values-in-top-hits-aggs/202983/2 "2019-11-07T08:59:48Z")

</div>

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