# Elasticsearch aggregation from springboot

**URL:** <https://discuss.elastic.co/t/elasticsearch-aggregation-from-springboot/275689>\
**Category:** Elasticsearch\
**Created:** [June 11, 2021, 1:50pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-from-springboot/275689 "2021-06-11T13:50:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saiteja](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@saiteja](https://discuss.elastic.co/u/saiteja)\
**Post date:** [June 11, 2021, 1:50pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-from-springboot/275689/1 "2021-06-11T13:50:58Z")

</div>

Hello,  
I am trying to get all the values for a filed with count using Aggregation Here is my code

```auto
Query searchQuery = new NativeSearchQueryBuilder()
                   .withQuery(QueryBuilders.matchAllQuery())
                   .addAggregation(AggregationBuilders.terms("ProgramNames").field("measurementName").order(BucketOrder.key(true)))
                   .build();
           
           SearchHits<Measurement> meas = elasticsearchTemplate.search(searchQuery, Measurement.class);

```

![error](https://us1.discourse-cdn.com/elastic/original/3X/c/3/c3c92425bf99ab955820764bad9d0c641fa899ed.png)  
i am able to get the docCount in bucket but not the keyAsString keyAsString is always null. am i looking correct places for the result.  
i am trying to build facets for measurementName field.  
mea(3)  
measR(4)  
meart(6)

---

<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:** [June 14, 2021, 1:48pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-from-springboot/275689/2 "2021-06-14T13:48:46Z")

</div>

Hey,

I haven't dug too deep in the code. I assume you ran `bucket.getKeyAsString()` before looking in the debug output? I just want to make sure, that there is no lazy initialization happening, as the `BytesRef` above named `key` seems not to be null.

Apart from that, this looks perfectly fine from a code perspective, I do have pretty much the same code in my own app.

--Alex

---

<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:** [July 12, 2021, 1:49pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-from-springboot/275689/3 "2021-07-12T13:49:18Z")

</div>

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