# Aggregations result to document with java client

**URL:** <https://discuss.elastic.co/t/aggregations-result-to-document-with-java-client/19907>\
**Category:** Elasticsearch\
**Created:** [September 22, 2014, 10:08am UTC](https://discuss.elastic.co/t/aggregations-result-to-document-with-java-client/19907 "2014-09-22T10:08:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zazasa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zazasa/32/22294_2.png) [@zazasa](https://discuss.elastic.co/u/zazasa)\
**Post date:** [September 22, 2014, 10:08am UTC](https://discuss.elastic.co/t/aggregations-result-to-document-with-java-client/19907/1 "2014-09-22T10:08:15Z")

</div>

Hi,  
I have a query that gives me a result like this:

> {  
> "took": 3,  
> "timed\_out": false,  
> "\_shards": {  
> "total": 8,  
> "successful": 8,  
> "failed": 0  
> },  
> "hits": {  
> "total": 8,  
> "max\_score": 0,  
> "hits":   
> },  
> "aggregations": {  
> "hmicro": {  
> "buckets": [  
> {  
> "key\_as\_string": "2",  
> "key": 2,  
> "doc\_count": 8  
> }  
> ]  
> },  
> "hmini": {  
> "buckets": [  
> {  
> "key\_as\_string": "0",  
> "key": 0,  
> "doc\_count": 8  
> }  
> ]  
> },  
> "hmacro": {  
> "buckets": [  
> {  
> "key\_as\_string": "1",  
> "key": 1,  
> "doc\_count": 8  
> }  
> ]  
> }  
> }  
> }

I want to take the entire "aggregations" content and put in as document. So  
i get the aggregations object

```
    JSONObject query = getQuery("statesQuery");

```

> ```
> SearchResponse sResponse = 
> 
> ```
> 
> remoteClient.prepareSearch(tribeIndex).setTypes("prc-i-state")  
> .setSource(query).execute().actionGet();
> 
> ```
> Aggregations aggs = sResponse.getAggregations();
> 
> ```

and i should use this to create the document:

```
    client.prepareIndex(runIndex_write, "state-hist")

```

> ```
> .setParent(runNumber)
> .setSource(aggs)
> .execute();   
> 
> ```

But i dont know how to transform aggs in the right object for the setSource  
function.

Could you help me ?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/9418c080-9807-410a-acef-5449cf583bc2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9418c080-9807-410a-acef-5449cf583bc2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:00am UTC](https://discuss.elastic.co/t/aggregations-result-to-document-with-java-client/19907/2 "2017-07-06T01:00:43Z")

</div>


