//更新文档
@Test
public void test4() throws IOException, InterruptedException, ExecutionException{
//指定ES集群
Settings settings = Settings.builder().put("cluster.name","haijines").build();
//创建访问es服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.42.159"),9300));
//更新文档
UpdateRequest request = new UpdateRequest();
request.index("index1")
.type("blog")
.id("10")
.doc(
XContentFactory.jsonBuilder().startObject()
.field("title","java查询elasticsearch文档")
.endObject()
);
UpdateResponse response = client.update(request).get();
System.out.println(response.status());
}
//upsert 如果不存在就会初始化
@Test
public void test5() throws IOException, InterruptedException, ExecutionException {
//指定ES集群
Settings settings = Settings.builder().put("cluster.name","haijines").build();
//创建访问es服务器的客户端
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.42.159"),9300));
//添加文档
IndexRequest request = new IndexRequest("index1","blog","8")
.source(
XContentFactory.jsonBuilder()
.startObject()
.field("id","2")
.field("title","java查询elasticsearch的数据")
.field("content","java查询elasticsearch的数据")
.field("postdate","2020-02-28")
.endObject()
);
UpdateRequest request2 = new UpdateRequest("index1","blog","8")
.doc(
XContentFactory.jsonBuilder().startObject()
.field("title","java查询elasticsearch文档")
.endObject()
).upsert(request);
UpdateResponse response = client.update(request2).get();
System.out.println(response.status());
}