4

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?

1 Answer 1

3

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).
Sign up to request clarification or add additional context in comments.

1 Comment

You can use jayway path to parse the SearchHit.getSource (which also integrates with Mappers like Jackson & SmartJSON to map back to a class/object, and vice-versa). I feel it's important to keep types agnostic for NoSQL databases like Elastic. Don't try too hard to make "many types".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.