# How to get \_id in search API in java high level rest client

**URL:** https://discuss.elastic.co/t/how-to-get-id-in-search-api-in-java-high-level-rest-client/222164
**Category:** Elasticsearch
**Created:** [March 4, 2020, 9:36pm UTC](https://discuss.elastic.co/t/how-to-get-id-in-search-api-in-java-high-level-rest-client/222164 "2020-03-04T21:36:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hemamalini](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@hemamalini](https://discuss.elastic.co/u/hemamalini)
#### Post date: [March 4, 2020, 9:36pm UTC](https://discuss.elastic.co/t/how-to-get-id-in-search-api-in-java-high-level-rest-client/222164/1 "2020-03-04T21:36:36Z")

</div>

Hi I am using Elastic search 5.6 . As of now i cannot upgrade to higher versions. I am using Java High level rest client to fetch documents . I need to get \_id in search response. I am not getting document id in search response.Please help to get the document id. I need to do bulk update of those documents later. so i need to get the document id. Also i need to get all the documents that match the result. Kindly help

```
SearchRequest searchRequest = new SearchRequest(DEEPDISCOVERYINDEX); 
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
		searchSourceBuilder.query(QueryBuilders.termQuery("is_new", "true")); 
		searchRequest.source(searchSourceBuilder);
	//	Header headers=null;
		/*
		 * try { 
                   SearchResponse searchResponse=restHighLevelClient.search(searchRequest,
		 * headers); 
         } catch (IOException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		//List<Object> deepdiscovery_doc =new ArrayList<Object>(); 
		try {
			  SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
			  SearchHits hits =searchResponse.getHits();
			  SearchHit[] searchHits = hits.getHits();
			  for(SearchHit hit : searchHits) {
				String sourceAsString =hit.getSourceAsString();
				System.out.println("sourxe"+sourceAsString);
				//JsonObject convertedObject = new Gson().fromJson(sourceAsString, JsonObject.class);
				Gson gson=new Gson();
				DeepDiscoveryDocument deepDiscoveryDoc=gson.fromJson(sourceAsString,DeepDiscoveryDocument.class);
				deepDiscoveryList.add(deepDiscoveryDoc);
			}
		}
		catch (IOException e) { // TODO Auto-generated catch block
			e.printStackTrace(); 
        }
```

---

<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: [March 5, 2020, 2:29am UTC](https://discuss.elastic.co/t/how-to-get-id-in-search-api-in-java-high-level-rest-client/222164/2 "2020-03-05T02:29:07Z")

</div>

I'm pretty sure you can do something like:

```
hit.getId();
```

---

<div class="post-metadata">

### Author: ![hemamalini](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@hemamalini](https://discuss.elastic.co/u/hemamalini)
#### Post date: [March 12, 2020, 2:27pm UTC](https://discuss.elastic.co/t/how-to-get-id-in-search-api-in-java-high-level-rest-client/222164/3 "2020-03-12T14:27:52Z")

</div>

Thanks a lot. It worked like a magic

---

<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: [April 9, 2020, 2:27pm UTC](https://discuss.elastic.co/t/how-to-get-id-in-search-api-in-java-high-level-rest-client/222164/4 "2020-04-09T14:27:54Z")

</div>

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