Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch return raw json with java api

I have following requirements in my spring web app:

  • find objects from Elasticsearch and display them on google map (json format preferred)
  • find objects (the same query as above) from Elasticsearch and display them on list (java objects format preferred to display it on JSP page)

I've written search with Java API using SearchRequestBuilder and it works fine:

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 prefer to just get JSON object from elasticsearch instead of SearchResponse object like this:

{
    "_index": "indexName",
    "_type": "type",
    "_id": "9094",
    "_version": 31,
    "found": true,
    "_source": {
        //list of properties
    }
}

Is it possible to get JSON response using Java API + SearchRequestBuilder or i have to use REST API for that?

like image 817
jgr Avatar asked Sep 07 '25 19:09

jgr


1 Answers

The Java api will not map to json (or any other entity for that matter) for you. However, you could do something like:

  • use spring-data-elasticsearch to deserialize directly to an entity
  • take the response from the Java api and parse to json using something like Jackson
  • consider using the jest Api which will return a gson (Googles json).
like image 190
hubbardr Avatar answered Sep 11 '25 05:09

hubbardr