# KeyAsString is null using aggregations

**URL:** <https://discuss.elastic.co/t/keyasstring-is-null-using-aggregations/164929>\
**Category:** Elasticsearch\
**Created:** [January 20, 2019, 10:23am UTC](https://discuss.elastic.co/t/keyasstring-is-null-using-aggregations/164929 "2019-01-20T10:23:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shira\_Bendor](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@Shira\_Bendor](https://discuss.elastic.co/u/Shira_Bendor)\
**Post date:** [January 20, 2019, 10:23am UTC](https://discuss.elastic.co/t/keyasstring-is-null-using-aggregations/164929/1 "2019-01-20T10:23:11Z")

</div>

Hi,  
I'm using Elastic 6.0.0.  
Using aggregations for the first time and I'm having some trouble with it.

My documents look something like this:

```
{
    ...
    "name" : "myname" (string),
    "result_count": 1234 (number) 
    ...
}

```

I'm getting results for multiple names each second.

What I want to achieve is to be able to decide for each name if the result count is low lately relative to the average.  
So I wanted to have the average per minute per name, and compare it to the average for this name across the last say 24 hours.

I did the following:

```
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("agg1").field("name.keyword").size(1000)
            .subAggregation(new DateHistogramAggregationBuilder("dateAgg")
                    .dateHistogramInterval(DateHistogramInterval.MINUTE)
                    .field("datetime")
                    .subAggregation(AggregationBuilders.avg("agg2").field("result-count")))
            .subAggregation(new ExtendedStatsAggregationBuilder("stats").field("result-count"));

```

So since "name" is a string I can't use it for the aggregation so I'm using the "keyword" of it.  
However when I get the response then I only see the keyword which is a bunch of hex values and the keyAsString is null,

I would like to understand how do I see the name itself i.e. the String that belongs to the same keyword?  
And any comment on what I'm doing will be more than welcome.

Thanks  
Shira

---

<div class="post-metadata">

**Author:** ![Shira\_Bendor](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@Shira\_Bendor](https://discuss.elastic.co/u/Shira_Bendor)\
**Post date:** [January 27, 2019, 9:47am UTC](https://discuss.elastic.co/t/keyasstring-is-null-using-aggregations/164929/2 "2019-01-27T09:47:50Z")

</div>

I figured it out, I needed to cast to the SubClass to be able to get the key:

e.g. ((Terms)(aggregations.get("agg1"))).getBuckets().get(0).getKey();

---

<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:** [February 24, 2019, 9:47am UTC](https://discuss.elastic.co/t/keyasstring-is-null-using-aggregations/164929/3 "2019-02-24T09:47:51Z")

</div>

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