Elasticsearch returns raw json with java api
I have the following requirements in my spring web application:
- find objects from Elasticsearch and display them on google map (preferred json format)
- find objects (same query as above) from Elasticsearch and display them in a list (java objects format is preferable to display on JSP page)
I wrote a Java API search using SearchRequestBuilder and it works great:
SearchRequestBuilder request = client.prepareSearch("index").setTypes("type")
.setSearchType(SearchType.QUERY_THEN_FETCH).setFrom(0).setSize(10).addFields(RESPONSE_FIELDS);
//request is much more complicated
//...
SearchResponse response = request.execute().actionGet();
SearchHits hits = response.getHits();
But for displaying it on google map, I would rather just get the JSON object from elasticsearch instead of the SearchResponse object like this:
{
"_index": "indexName",
"_type": "type",
"_id": "9094",
"_version": 31,
"found": true,
"_source": {
//list of properties
}
}
Is it possible to get a JSON response using Java API + SearchRequestBuilder or should I use the REST API for this?
+3
source to share
1 answer
Java api won't show up for json (or any other object for that matter) for you. However, you can do something like:
- use spring-data-elasticsearch to deserialize directly for entity
- take answer from Java api and parse json using something like Jackson
- consider using jest Api which will return gson (Googles json).
+4
source to share