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

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(); 
        }

I'm pretty sure you can do something like:

hit.getId();

Thanks a lot. It worked like a magic

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