I have an elasticsearch 8.1 server running to which I want to post JSONObjects. I build my IndexRequest using the Java API as follows:
JSONObject asset = (JSONObject) a;
IndexRequest<JSONObject> req;
req = IndexRequest.of(b->b.index("assets").document(asset).id(asset.getString("symbol")));
ESClient.index(req)
The ESClient is built like this:
public ElasticsearchClient client() {
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200)).build();
ElasticsearchTransport transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
return new ElasticsearchClient(transport);
}
The JSONObject (org.json.JSONObject) asset is quite big, and it is definitely not empty. When I query the index, the relevant part of the result look like this:
"hits" :
[
{
"_index" : "assets",
"_id" : "BTC",
"_score" : 1.0,
"_source" : {
"empty" : false
}
}
]
It looks as if the document got uploaded, but nothing of the body. What am I doing wrong here? All help appreciated.
EDIT:
It works when writing the JSONObject to a file and posting a FileReader of that file, but that is in no way a solution to my problem:
JSONObject asset = (JSONObject) a;
File file = new File("asset");
FileWriter fileWriter = new FileWriter(file);
fileWriter.append(asset.toString());
fileWriter.close();
FileReader fileReader = new FileReader("asset");
IndexRequest<JsonData> req;
req = IndexRequest.of(b->b.index("assets").withJson(fileReader).id(asset.getString("symbol")));
fileReader.close();
ESClient.index(req)