3

I want to convert the elasticsearch search result to Json Object. I havent found any proper way to convert directly.

SearchResponse response = client.prepareSearch(index).setExplain(true).execute().actionGet(); response->JSON Object. 

Is there any way to convert an ElasticSearch response to a Json Object?

0

2 Answers 2

3

In Java, you can directly convert the SearchResponse to JSONObject. Below is the handy code.

SearchResponse SR = builder.setQuery(QB).addAggregation(AB).get(); JSONObject SRJSON = new JSONObject(SR.toString()); 
Sign up to request clarification or add additional context in comments.

1 Comment

I have using the same but it is saying... constructor JSONObject(String) undefined any Idea. SearchResponse res = client.prepareSearch(_index).setSize(0).addAggregation(aggregation).execute().actionGet(); JSONObject SRJSON = new JSONObject(res.toString());
1

You need to use the SearchResponse.toXContent() method like this:

SearchResponse response = client.prepareSearch(index).setExplain(true).execute().actionGet(); XContentBuilder builder = XContentFactory.jsonBuilder(); response.toXContent(builder, ToXContent.EMPTY_PARAMS); JSONObject json = new JSONObject(builder.string()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.