# Elasticsearch javaApi

**URL:** <https://discuss.elastic.co/t/elasticsearch-javaapi/51528>\
**Category:** Elasticsearch\
**Created:** [June 1, 2016, 8:41am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528 "2016-06-01T08:41:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![omar\_romdhane](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@omar\_romdhane](https://discuss.elastic.co/u/omar_romdhane)\
**Post date:** [June 1, 2016, 8:41am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/1 "2016-06-01T08:41:51Z")

</div>

hi , when i use searsh-api to retrieve data from one index using this code :

> Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch-cluster").build();  
> client = TransportClient.builder().settings(settings).build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.206.206.82"), 9300));

> SearchResponse sr = client.prepareSearch("logstash-2016.05.31").get();

i got a json contains a data but also non-useful data at the beginning of json like this

{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 110,  
"max\_score" : 1.0,  
"hits" : [

plz how eliminate it ?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 1, 2016, 9:31am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/2 "2016-06-01T09:31:54Z")

</div>

This? [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/common-options.html#\_response\_filtering](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/common-options.html#_response_filtering)

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [June 1, 2016, 9:39am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/3 "2016-06-01T09:39:04Z")

</div>

You can do response filtering using REST api, but I'm not sure the java api supports this. According to [How to use filter\_path in java api?](https://discuss.elastic.co/t/how-to-use-filter-path-in-java-api/42844) you can't use it.

---

<div class="post-metadata">

**Author:** ![tanguy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tanguy/32/6030_2.png) [@tanguy](https://discuss.elastic.co/u/tanguy)\
**Post date:** [June 1, 2016, 9:53am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/4 "2016-06-01T09:53:53Z")

</div>

You might be interested in this comment too: [https://github.com/elastic/elasticsearch/pull/10980#issuecomment-190107941](https://github.com/elastic/elasticsearch/pull/10980#issuecomment-190107941)

---

<div class="post-metadata">

**Author:** ![omar\_romdhane](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@omar\_romdhane](https://discuss.elastic.co/u/omar_romdhane)\
**Post date:** [June 1, 2016, 10:58am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/5 "2016-06-01T10:58:54Z")

</div>

i need to eliminate the header

> {  
> "took" : 1,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> }

and informations about returned results

> "hits" : {  
> "total" : 110,  
> "max\_score" : 1.0,

so i want to display just the hits array, which includes the returned data

this is a part of the response

{  
"took" : 2,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 25,  
"successful" : 10,  
"failed" : 0  
},  
"hits" : {  
"total" : 200,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "logstash-2016.05.30",  
"\_type" : "log4j",  
"\_id" : "AVUCj6LyBTC2ElQBKf9k",  
"\_score" : 1.0,  
"\_source" : {  
"message" : " retrieveValidGatewayTransmissionData :put dupsClosedMap !",  
"@version" : "1",  
"@timestamp" : "2016-05-30T16:46:54.391Z",  
"timestamp" : 1464626814678,  
"path" : "com.esprit.HelloWorldServlet",  
"priority" : "WARN",  
"logger\_name" : "com.esprit.HelloWorldServlet",  
"thread" : " [task-scheduler-5] ",  
"class" : "?",  
"file" : "?:?",  
"method" : "?",  
"host" : "10.206.206.72:51303",  
"type" : "log4j",  
"date" : "016-03-23 16:32:02.613 ",  
"lvl" : " W ",  
"gateway" : "GTW2",  
"device" : "Device2",  
"source" : " LogService:14 "  
}  
}, {  
"\_index" : "logstash-2016.05.30",  
"\_type" : "log4j",  
"\_id" : "AVUCj6LyBTC2ElQBKf9l",  
"\_score" : 1.0,  
"\_source" : {  
"message" : " retrieveValidGatewayTransmissionData :put dupsClosedMap !",  
"@version" : "1",  
"@timestamp" : "2016-05-30T16:46:54.395Z",  
"timestamp" : 1464626814678,  
"path" : "com.esprit.HelloWorldServlet",  
"priority" : "ERROR",  
"logger\_name" : "com.esprit.HelloWorldServlet",  
"thread" : " [task-scheduler-6] ",  
"class" : "?",  
"file" : "?:?",  
"method" : "?",  
"host" : "10.206.206.72:51303",  
"type" : "log4j",  
"date" : "016-03-23 17:32:02.613 ",  
"lvl" : " E ",  
"gateway" : "GTW3",  
"device" : "Device5",  
"source" : " LogService:23 "  
}  
}]  
}

---

<div class="post-metadata">

**Author:** ![omar\_romdhane](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@omar\_romdhane](https://discuss.elastic.co/u/omar_romdhane)\
**Post date:** [June 1, 2016, 11:18am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/6 "2016-06-01T11:18:05Z")

</div>

i need to returns just the `_source` , without metadata  
🙂

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [June 1, 2016, 1:17pm UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/7 "2016-06-01T13:17:06Z")

</div>

As @tanguy pointed out, you can take the search response (sr) and use its `#toXContent()` method to write it to an XContentBuilder that allies filters. Here is an example that gives you `hits.hits._source`

```
XContentBuilder filteredBuilder = XContentBuilder.builder(JsonXContent.jsonXContent, new String[]{"hits.hits._source"}).prettyPrint();
filteredBuilder.startObject();
sr.toXContent(filteredBuilder, ToXContent.EMPTY_PARAMS);
filteredBuilder.endObject();
System.out.println(filteredBuilder.string());

```

The result looks something like this:

```
{
  "hits" : {
    "hits" : [ {
      "_source" : {
        "value" : 2,
        "text" : "something"
      }
    }, {
      "_source" : {
        "value" : 4,
        "text" : "something else"
      }, 
     ...
   } ]
  }
}

```

Note however this only changes presentation of the result. Since the filtering is done on the search response, all data that is filtered out is still transported to the client.

---

<div class="post-metadata">

**Author:** ![omar\_romdhane](https://avatars.discourse-cdn.com/v4/letter/o/8dc957/32.png) [@omar\_romdhane](https://discuss.elastic.co/u/omar_romdhane)\
**Post date:** [June 2, 2016, 7:58am UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/8 "2016-06-02T07:58:10Z")

</div>

thanks chuescher ,

i used this code to extract only the data

```
> SearchResponse sr = client.prepareSearch("logstash-*")

```

> ```
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
> .setFetchSource(true)
> .setSize(1000)
> .setFetchSource( new String[] {"lvl","message","date","device","thread","source","gateway"}, null)		
> // .addFields("lvl")
> .execute()
> .actionGet();
>   
>   
> for (SearchHit hit : sr.getHits()){
> map = hit.getSource();
> }
> 
> ```

---

<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:47pm UTC](https://discuss.elastic.co/t/elasticsearch-javaapi/51528/9 "2017-07-05T22:47:00Z")

</div>


