Bulk request how to?

Hi I'm a bit confused on how to perform a bulk request with the new Java API client.

To index one document I use the following code that works without problems. Since we are using GSON I pass the json string representation of the document in a StringReader (country.toESJsonReader()):

	IndexResponse indexResponse = elasticSearch.getESClient().index(builder -> 
		builder
			.index(aliasName)
			.id(country.id())
			.withJson(country.toESJsonReader())
			.waitForActiveShards(asBuilder -> asBuilder.count(1))
			.refresh(Refresh.True));

When indexing multiple documents I use the following code that throws the following exception:
"Class class co.elastic.clients.Elasticsearch.core.bulk.CreateOperation cannot be read from JSON"

	// Create bulk request.
	BulkRequest.Builder bulkRequestBuilder = new BulkRequest.Builder()
		.waitForActiveShards(asBuilder -> asBuilder.count(1))
		.refresh(Refresh.True);

	// Add countries to bulk request.
	List<BulkOperation> bulkOperations = new ArrayList<>();
	for (Country country : countries) {
		bulkOperations.add(new CreateOperation.Builder<String>()
			.index(aliasName)
			.id(country.id())
			.withJson(country.toESJsonReader())
			.build()
			._toBulkOperation());					
	}
	bulkRequestBuilder.operations(bulkOperations);
				
	// Perform bulk request.
	BulkResponse bulkResponse = elasticSearch.getESClient().bulk(bulkRequestBuilder.build());

Any idea what I am doing wrong here?

Thanks

Hi.
Read this post, it seems to be similar to your problem and managed to solve it by another approach.

Hi Andre,

Thank you very much! That did the trick. It's apparently a bug in the API. Hopefully it will be fixed soon.

1 Like

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