'Elasticsearch High-level Rest Client: BulkRequest not retaining value outside of For loop
I am trying to use an Elasticsearch BulkRequest. I believe I am having some sort of scope issue, which I cannot for the life of me understand.
public void saveAll(Map<I, T> saveRequestMap) {
BulkRequest request = new BulkRequest();
for(Map.Entry<I, T> document : saveRequestMap.entrySet()) {
request.add(indexRequest(document.getKey(), document.getValue()));
// request.size() here shows the correct number of items
}
// request.size() here always shows 0 items (should show 50 if 50 items were added above)
BulkResponse response = client.bulk(request, DEFAULT_REQUEST_OPTIONS());
}
protected final IndexRequest indexRequest(I id, T entity) {
return new IndexRequest(indexName)
.id(id.toString())
.source(mapEntityToJsonString(entity), XContentType.JSON);
}
private mapEntityToJson(T entity) {
String jsonObject = mapper.toJsonString(entity);
log.trace("mapped object: {}", jsonOBject);
return jsonObject;
}
If I have 50 items being bulk-indexed, it will fail because Elasticsearch claims there are 0 items. I soon realized that request.add() is not working like I thought it should and the request is losing all IndexRequests created in the for loop.
If I change the code to this, it is able to index properly:
public void saveAll(Map<I, T> saveRequestMap) {
BulkRequest request = new BulkRequest();
int count = 0;
for(Map.Entry<I, T> document : saveRequestMap.entrySet()) {
request.add(indexRequest(document.getKey(), document.getValue()));
// request.size() here shows the correct number of items
count++;
if(count == saveRequestMap.size()) {
// request.size() here shows the correct number of items (e.g. 50)
BulkResponse response = client.bulk(request, DEFAULT_REQUEST_OPTIONS());
}
}
}
protected final IndexRequest indexRequest(I id, T entity) {
return new IndexRequest(indexName)
.id(id.toString())
.source(mapEntityToJsonString(entity), XContentType.JSON);
}
private mapEntityToJson(T entity) {
String jsonObject = mapper.toJsonString(entity);
log.trace("mapped object: {}", jsonOBject);
return jsonObject;
}
So the question I have:
- Why does request not retain the objects added to it outside of the for loop, even though it was declared outside of the loop?
I have looked at several posts here at SO and haven't found anything that really helps me understand or hint at whats going on.
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
