SearchResponse<ObjectNode> searchResponse = elasticsearchClient.search(searchRequest, ObjectNode.class);
Hit<ObjectNode> hit = searchResponse.hits().hits().get(0);
Map<String, JsonData> fields = hit.fields();
JsonData data = fields.get("somefield");
What is the correct way to turn 'data' into a json string for further processing?
Per chance are you using the Elasticsearch Java client?
Have you tried looking at the
JsonpMapper to convert
data into an object for further processing as suggested in the documentation?
A raw JSON value. It can be converted to a JSON node tree or to an arbitrary object using a
Thanks. Yes I'm using the Java client. And yes I read the doc. It also says " Instances of this class returned by API clients keep a reference to the client's
JsonpMapper and can be converted to arbitrary types using
to(Class) without requiring an explicit mapper." I ran a test and I'm able to use the following to get the json string from a JsonData.
String json = fields.get("thefield").toJson().toString();
Using this doesn't seem to work:
String json = fields.get("thefield").to(String.class);
Looking at the JavaDoc for
to(Class) it states that you need to specify a mapper:
Converts this object to a target class. A mapper must have been provided at creation time.
java.lang.IllegalStateException - if no mapper was provided at creation time.
Can you see if specifying an explicit mapper works?
Am I missing something? It says I shouldn't have to if the instance was returned by the API client - whiich it was.
I'm slightly confused myself. Can you share the link to the location in the docs where it says an explicit mapper isn't needed?
Specifically I'm trying to figure out how do access the value of a stored field. It used to be like this with the HLRC:
DocumentField field = fields.get("thefield");
String value = field.getValue();
JsonData jsonData = fields.get("thefield");
// How to get the first string value?
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.