# Elasticsearch java api field extracting

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457>\
**Category:** Elasticsearch\
**Created:** [April 26, 2016, 4:20pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457 "2016-04-26T16:20:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maher](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@maher](https://discuss.elastic.co/u/maher)\
**Post date:** [April 26, 2016, 4:20pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457/1 "2016-04-26T16:20:13Z")

</div>

Hi,  
I am trying to retrieve some data from elasticsearch using java api.

SearchResponse response = client.prepareSearch("dashboard")  
.setTypes("logs")  
.setFetchSource(new String[]{"aggregations"}, null)  
.addAggregation(AggregationBuilders.terms("DistincProduct").field("productcode"))  
.execute()  
.actionGet();

Is there a way to get a specifig field from the response like the aggregation field...  
(i know about parsing the json text, I am just asking about a common and better solution you are using)

Thank you in advence,

---

<div class="post-metadata">

**Author:** ![Bruce\_Ritchie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_ritchie/32/9370_2.png) [@Bruce\_Ritchie](https://discuss.elastic.co/u/Bruce_Ritchie)\
**Post date:** [April 26, 2016, 5:21pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457/2 "2016-04-26T17:21:00Z")

</div>

If you include a field in the query it's available in the response for each search hit:

```
SearchRequestBuilder requestBuilder = client.prepareSearch("dashboard");
requestBuilder.addField("field1");
requestBuilder.addField("field2");
requestBuilder.setQuery(QueryBuilders.queryStringQuery("someQueryString").field("someField"));
SearchResponse response = requestBuilder.execute().actionGet(timeout);
SearchHits hits = response.getHits();

for (SearchHit result : hits) {
    Map<String, SearchHitField> fields = result.getFields();
    SearchHitField field = fields.get("field1");
    ....
}

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 26, 2016, 7:16pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457/3 "2016-04-26T19:16:42Z")

</div>

So you want to run the search but only get the aggregation back? Set the `size` to 0. In the master branch that method is called `setSize`. The aggregation should be available in the `getAggregations` method. The REST API has response filtering but I believe that is a thing implemented on the REST layer so it doesn't have an analog in the java API.

---

<div class="post-metadata">

**Author:** ![maher](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@maher](https://discuss.elastic.co/u/maher)\
**Post date:** [April 27, 2016, 8:55am UTC](https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457/4 "2016-04-27T08:55:10Z")

</div>

Thank you guys,

Bruce\_Ritchie that helped me a lot with getting the exact field in a search using a query.

nik9000, i used the size to reduce the hits wich i dont need for now , but is there a way to ensure that i get all the hits when i use a filter ?

And to get the aggregation fields this worked well for me:

```
  		Terms terms = response.getAggregations().get("aggsName");
	        Collection<Terms.Bucket> buckets = terms.getBuckets();
	
                for (Bucket bucket : buckets) {
       System.out.println(bucket.getKeyAsString() +" ("+bucket.getDocCount()+")");
       }

```

I love your team 😃

---

<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 5, 2017, 10:56pm UTC](https://discuss.elastic.co/t/elasticsearch-java-api-field-extracting/48457/5 "2017-07-05T22:56:02Z")

</div>


