Elasticsearch Java API 的使用(4)—更新索引(update & upset),elasticsearchapi
Java更新索引(update & upset)
update
更新使用UpdateRequest(update类型更新,只能更新)
public class EsUpdate{
public void updateIndex(TransportClient client){
Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2016-7-21 00:00:01");
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index("pointdata")
.type("pointdata")
.id("1")
.doc(XContentFactory.jsonBuilder()
.startObject()
.field("pointid","W3.UNIT1.10LBG01CP302")
.field("pointvalue","0.8029")
.field("inputtime",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
.endObject()
);
//执行修改
UpdateResponse response1 = client.update(updateRequest).get();
//查询修改状态,返回ok表示成功
System.out.println(response1.status());
}
}
upset
要用IndexRequest设定添加文档,UpdateRequest设定更新文档,设定upset执行有则修改无则更新(upset类型更新,文档不存在时创建)
public class EsUpSet{
public void updateIndex(TransportClient client){
Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2016-7-21 00:00:01");
//添加文档
IndexRequest request1 = new IndexRequest("pointdata", "pointData", "1")
.source(
XContentFactory.jsonBuilder()
.startObject()
.field("pointid","W3.UNIT1.10LBG01CP302")
.field("pointvalue","0.8029")
.field("inputtime",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
.endObject()
);
//修改文档
UpdateRequest updateRequest2 = new UpdateRequest();
updateRequest.index("pointdata")
.type("pointdata")
.id("1")
.doc(XContentFactory.jsonBuilder()
.startObject()
.field("pointid","W3.UNIT1.10LBG01CP302")
.field("pointvalue","0.8029")
.field("inputtime",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time))
.endObject()
).upset(request1);
UpdateResponce responce = client.update(request2).get();
//查询修改状态,返回ok表示成功
System.out.println(response2.status());
}
}
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。