Get index number in the bucket of specific key

"daily_rank": {
  "buckets": [
    {
      "key_as_string": "2018-08-20T00:00:00.000Z",
      "key": 1534723200000,
      "doc_count": 2878,
      "game": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "a",
            "doc_count": 1439,
            "count": {
              "value": 689593
            }
          },
          {
            "key": "b",
            "doc_count": 1439,
            "count": {
              "value": 405340
            }
          }
        ]
      },
      "count": {
        "value": 2
      }
    }
}

I have a query return like above.
How can I get the index number of "key=a" in buckets?

I try to use this
def list = ctx.payload.aggregations.daily_rank.buckets.0.game.buckets;
def index = list.stream().filter(x -> "a".equals(list.get(x).getKey())).findFirst();

But it return java.util.HashMap cannot be cast to java.lang.Number?

Hey,

if you only interested in the a bucket, a filter in your query might make sense as well, as this would potentially speed up the query.

I'd be interested in the use-case why you need the exact index. Otherwise just filtering out the others might help as well?

Also, the a key could occur more often than once, because it is already in a nested aggregation.

--Alex

Currently I am making a leaderboard.
I aggregate 2 days before and 1 day before data.
And use the index as the rank level.
Inside the aggregation, It might have lots of objects.
I can have last day rank compare with a day before rank to see the trend.
So last day the index number of "a" might be 1, and a day before might be 5.
Then I can know the trend is +4.
That's what I want to achieve.
But I am not familiar with Java, so just stuck there.

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