# Get index number in the bucket of specific key

**URL:** <https://discuss.elastic.co/t/get-index-number-in-the-bucket-of-specific-key/145524>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [August 22, 2018, 9:36am UTC](https://discuss.elastic.co/t/get-index-number-in-the-bucket-of-specific-key/145524 "2018-08-22T09:36:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Izek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/izek/32/37113_2.png) [@Izek](https://discuss.elastic.co/u/Izek)\
**Post date:** [August 22, 2018, 9:36am UTC](https://discuss.elastic.co/t/get-index-number-in-the-bucket-of-specific-key/145524/1 "2018-08-22T09:36:19Z")

</div>

```json
"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?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 22, 2018, 12:59pm UTC](https://discuss.elastic.co/t/get-index-number-in-the-bucket-of-specific-key/145524/2 "2018-08-22T12:59:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Izek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/izek/32/37113_2.png) [@Izek](https://discuss.elastic.co/u/Izek)\
**Post date:** [August 22, 2018, 2:00pm UTC](https://discuss.elastic.co/t/get-index-number-in-the-bucket-of-specific-key/145524/3 "2018-08-22T14:00:46Z")

</div>

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.

---

<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:** [September 19, 2018, 2:00pm UTC](https://discuss.elastic.co/t/get-index-number-in-the-bucket-of-specific-key/145524/4 "2018-09-19T14:00:46Z")

</div>

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