Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import io.searchbox.core.Update;
import io.searchbox.core.Bulk;
import io.searchbox.core.Bulk.Builder;
import io.searchbox.action.BulkableAction;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
//initialise objects required to do bulk updates
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://"+ElasticSearch_IP+":9200")
.multiThreaded(true)
.build());
client = factory.getObject();
current_bulkable_action = 0;
bulkProcessor = new Bulk.Builder();
// the below assumes "hit" comes from the ES document you'd like to update
String ES_ID = this.hit.get("_id").getAsString();
String ES_INDEX = this.hit.get("_index").getAsString();
String ES_TYPE = this.hit.get("_type").getAsString();
jsonUpdateObject = jsonBuilder().startObject().startObject("doc");
jsonUpdateObject
.field("foo", "bar")
.field("john", "doe");
jsonUpdateObject.endObject().endObject();
update_builder = new Update.Builder(this.jsonUpdateObject.string()).index(ES_INDEX).id(ES_ID).type(ES_TYPE).build());
bulkProcessor.addAction(update_builder);
current_bulkable_action += 1;
if (current_bulkable_action > 1000) {
try {
System.out.println("Flushing BULK processor");
client.execute(bulkProcessor.build());
current_bulkable_action = 0;
} catch (IOException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment