HashMap<String, Object> to JsonData in new elasticsearch Java client API

How to convert a hashmap to co.elastic.clients.json.JsonData in new elasticsearch Java client API?

I am doing this conversion to provide the document to a IndexRequest.Builder.

IndexRequest.Builder<JsonData> request =
        new IndexRequest.Builder<JsonData>().index(indexName).id(1);

Is it required to convert the hashmap to Json string first, then to JsonData?
or is there a way to directly convert hashMap to JsonData that can be feed to the IndexRequest?

You can create a JsonData object from any object that can be serialized to JSON using JsonData.of(theMap).

Now in your use case you don't need JsonData and can also create IndexRequest.Builder<HashMap<String, Something>> and use request.document(theMap).

1 Like


Thank you for this information.

Appreciate it much. :+1:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.