Update - TuPengXiong/TuPengXiong.github.io GitHub Wiki
package tpx.elasticsearch;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
public class Update {
public static void main(String[] args) {
Update update = new Update();
Index index = new Index();
index.createTransportClient();
printResponse(update.update(index.client,"tpx", "testBuilder", "1"));
index.closeTransportClient();
}
public UpdateResponse update(TransportClient client,String index,String shard,String id) {
UpdateResponse response = null;
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(index);
updateRequest.type(shard);
updateRequest.id(id);
try {
updateRequest.doc(XContentFactory.jsonBuilder()
.startObject()
.field("gender", "male"+new Date())
.endObject());
response = client.update(updateRequest).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
public static void printResponse(UpdateResponse response){
System.out.println(response.getVersion());
}
}