Elasticsearch java Rest API

'''public Map<String, Object> view(@PathVariable final String id) throws IOException {

	    GetRequest request = new GetRequest("company", "employee", id).version(1);
	    GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
	    
	    return getResponse.getSource();

	}'''

For this coding it return Json object
But,

''' public String datesearch2() throws IOException
{
SearchRequest searchRequest = new SearchRequest();

	 searchRequest.indices("winlogbeat-6.5.3-2019.02.01");
	 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
	 searchSourceBuilder.query(QueryBuilders.rangeQuery("@timestamp").from("2019-02-01T10:28:13.338Z").to("2019-02-01T10:29:13.385Z")); 
	 searchRequest.source(searchSourceBuilder); 
	 
	 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

	return searchResponse.toString();
	
	 
    }'''

For this it gives Json as a String in page what i need to do to get as Json object?

What is a json object?

The source of you object is in the response :

for(SearchHit hit : searchResponse.getHits().getHits()) {
    // HERE IS THE SOURCE OF THE DOCUMENT
    hit.getSourceAsString()
}

Is it what you are looking for ?

But i need as Json look like first result?
for that what i need to do?

May be send back to your browser an application/json content-type?
But that's outside the scope of elasticsearch as this is a problem in your application and the way you are sending REST response if I understand correctly.

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