I am inserting JSON data in Elasticsearch using ElasticearchClient Bulk Insert in Java, the code I have written is
static void submitBulkUsingElasticClient(JSONArray data) throws IOException {
BulkRequest.Builder builder = new BulkRequest.Builder();
ElasticsearchClient esClient = getElasticsearchClient();
for (Object document : data)
if (document instanceof JSONObject) {
JSONObject jsonObject = new JSONObject(document);
builder.operations(op -> op
.index(idx -> idx
.index("demo_data")
.document(jsonObject.toMap())
.id(String.valueOf(((JSONObject) document).getInt("id")))
)
);
}
BulkResponse response = esClient.bulk(builder.build());
// Log errors, if any
if (response.errors()) {
System.out.println("Bulk had errors");
for (BulkResponseItem item : response.items()) {
if (item.error() != null) {
System.out.println(item.error().reason());
}
}
}
System.out.println("response = " + response.toString());
}
And the sample JSON data I have taken is this,
During single data insert data is populating and visualizing in Kibana properly. Here is the example for single data insertion,
Here is the code of Single data Insertion,
static void submitRequestUsingElasticClient(String data) throws IOException {
Reader input = new StringReader(data);
IndexRequest<JsonData> request = IndexRequest.of(i -> i
.index("demo_data")
.withJson(input)
);
ElasticsearchClient esClient = getElasticsearchClient();
IndexResponse response = esClient.index(request);
System.out.println("Indexed with version " + response.version());
}
But during bulk insertion the data is getting inserted successfully but not in proper format, its looking something like this,
I don't understand the problem why the format is like this. Is it because Elasticsearch is serializing the data or is there any field missing in BulkRequest Builder operation?
Please help me..