![Elasticsearch技术解析与实战](https://wfqqreader-1252317822.image.myqcloud.com/cover/554/851554/b_851554.jpg)
上QQ阅读APP看书,第一时间看更新
1.6.4 修改文档
修改文档有两种方式,一种是直接修改,另一种是如果文档不存在则插入存在再修改。
第一种方式的代码:
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.index(indexName);
updateRequest.type(type);
updateRequest.id("1");
updateRequest.doc(jsonBuilder()
.startObject().field("type", "file").endObject());
client.update(updateRequest).get();
第二种方式的代码:
IndexRequest indexRequest = new IndexRequest(indexName, type, "3") .source(jsonBuilder() .startObject() .field("type", "syslog") .field("eventCount", 2) .field("eventDate", new Date()) .field("message", "secilog insert doc test") .endObject()); UpdateRequest updateRequest = new UpdateRequest(indexName, type, "3") .doc(jsonBuilder().startObject().field("type", "file").endObject()) .upsert(indexRequest); client.update(updateRequest).get();