Inherited Geek
Inherited Geek

Reputation: 2383

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);

Upvotes: 2

Views: 1584

Answers (2)

Nik Red
Nik Red

Reputation: 11

I know It's an old question, but just in case someone stumbles across this, here is a java 8/(lambdas) way of doing the same thing.

    Client.execute( new Bulk.Builder()
                         .addAction(
                           bills.stream()
                             .map(bill -> 
                               new Index.Builder(bill.toString()
                             )
                             .index(index).type(type).id(id).build())
                             .collect(Collectors.toList())
                        ).build());

Upvotes: 1

Val
Val

Reputation: 217274

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());

Upvotes: 4

Related Questions