Can I access the field values in top hits aggs?

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?

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