javaelasticsearchelasticsearch-jest

JEST Bulk Request Issue


I am trying to run a Bulk Request through JEST and want to append my data (say "bills") one at a time and then execute all at once, however when i run the following code on 10 bills just the last bill is getting executed, can someone please correct this code to execute all 10 bills (by executing it outside the for loop ie using Bulk Request)?

    for(JSONObject bill : bills) {          
                 bulkRequest = new Bulk.Builder()
                .addAction(new Index.Builder(bill.toString()).index(index).type(type).id(id).build())
                .build();
                }

        bulkResponse = Client.execute(bulkRequest);

Solution

  • You need to build the Bulk Builder out of the loop and then use it to add all bills:

    bulkRequest = new Bulk.Builder()
    for(JSONObject bill : bills) {          
          bulkRequest.addAction(new Index.Builder(bill.toString()).index(index).type(type).id(id).build())
    }    
    bulkResponse = Client.execute(bulkRequest.build());