-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Improve Deserialization of TasksRecord in Elasticsearch Java Client #896
Comments
Hello! We have existing utilities to directly convert Elasticsearch client types to Maps, but we do have some other utilities that we can leverage to avoid string manipulation. Here's what I would to to convert a list of JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper(); // using Jackson mapper because it's the one we recommend
ElasticsearchTransport transport = new RestClientTransport(restClient, jsonpMapper); // restClient required of course
ElasticsearchClient esClient = new ElasticsearchClient(transport); //instance of the client
List<TasksRecord> taskList = esClient.cat().tasks().valueBody(); // getting the task records like you did
JsonProvider provider = JsonpUtils.provider(); // this is available from the client utilities, no need to import anything else
List<HashMap> maps = taskList.stream().map(task -> {
StringWriter sw = new StringWriter(); // where the serialized json string will be written
JsonGenerator generator = provider.createGenerator(sw);
task.serialize(generator,jsonpMapper); // this just outputs the json, no class name or other characters
generator.close(); // flushing the buffer into the string writer
return jsonpMapper.deserialize(jsonpMapper.jsonProvider().createParser(new StringReader(sw.toString())),HashMap.class); // deserializing the json string to a map
}).toList(); The result will be a |
Description:
Code Snippet: IndexTemplateItem indexTemplateItem = client.indices().getIndexTemplate(t -> t.name(indexTemplate)).indexTemplates().get(0);
JacksonJsonpMapper jsonpMapper = new JacksonJsonpMapper(); // Using Jackson mapper as recommended
StringWriter sw = new StringWriter(); // Where the serialized JSON string will be written
JsonProvider provider = JsonpUtils.provider(); // Available from the client utilities
JsonGenerator generator = provider.createGenerator(sw);
indexTemplateItem.serialize(generator, jsonpMapper); // Outputs the JSON
generator.close(); // Flushes the buffer into the string writer
return jsonpMapper.deserialize(jsonpMapper.jsonProvider().createParser(new StringReader(sw.toString())), HashMap.class); // Deserializing the JSON string to a map Expected Behavior: Actual Behavior: Additional Context: |
Just replace JsonProvider provider = JsonpUtils.provider(); with JsonProvider provider = jsonpMapper.jsonProvider(); |
elsaticsearch: 8.11.4
elasticsearch-java: 8.11.4
java8
Issue: How to Improve Deserialization of
TasksRecord
in Elasticsearch Java ClientI am querying task records from Elasticsearch using the Java client like this:
Currently, I am attempting to deserialize these
TasksRecord
objects into aMap
using the following approach:In this code:
TasksRecord
.FastJsonUtils.toObject()
to deserialize the modified string into aMap
.I believe this solution is not ideal because it relies heavily on string manipulation, which can be error-prone and inefficient.
My main question is: How can I improve the deserialization process?
TasksRecord
into aMap
or another structure, leveraging existing Elasticsearch client functionality?I would greatly appreciate any advice or best practices for optimizing this code.
The text was updated successfully, but these errors were encountered: