使用 Java 客户端对 ElasticSearch 文档进行删改查
2011 年 7 月 19 日
松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里–> Spring Boot+Vue+微人事视频教程
前面和小伙伴们分享了通过 Java 客户端来添加 Es 文档,整体操作还是非常 Easy,今天我们来看看通过 Java 客户端对 Es 文档进行删改查。
以下是视频笔记:
注意,笔记只是视频内容的一个简要记录,因此笔记内容比较简单,完整的内容可以查看视频。
29.2 获取文档
根据 id 获取文档:
public class GetDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
GetRequest request = new GetRequest("book", "98");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
if (response.isExists()) {
//如果文档存在
long version = response.getVersion();
System.out.println("version = " + version);
String sourceAsString = response.getSourceAsString();
System.out.println("sourceAsString = " + sourceAsString);
}else{
System.out.println("文档不存在");
}
client.close();
}
}
29.3 判断文档是否存在
判断文档是否存在和获取文档的 API 是一致的。只不过在判断文档是否存在时,不需要获取 source。
public class ExistsDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
GetRequest request = new GetRequest("book", "99");
request.fetchSourceContext(new FetchSourceContext(false));
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println("exists = " + exists);
client.close();
}
}
29.4 删除文档
删除 id 为 99 的文档:
public class DeleteDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
DeleteRequest request = new DeleteRequest("book", "99");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
System.out.println("response.getVersion() = " + response.getVersion());
ReplicationResponse.ShardInfo shardInfo = response.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
System.out.println("有分片存在问题");
}
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
System.out.println("failure.reason() = " + failure.reason());
}
}
client.close();
}
}
删除文档的响应和添加文档成功的响应类似,可以对照着理解。
29.4 更新文档
通过脚本更新:
public class UpdateDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
UpdateRequest request = new UpdateRequest("book", "1");
//通过脚本更新
Map params = Collections.singletonMap("name", "三国演义666");
Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.name=params.name", params);
request.script(inline);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
System.out.println("response.getVersion() = " + response.getVersion());
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("更新成功!");
}
client.close();
}
}
通过 JSON 更新:
public class UpdateDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
UpdateRequest request = new UpdateRequest("book", "1");
request.doc("{\"name\": \"三国演义\"}", XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
System.out.println("response.getVersion() = " + response.getVersion());
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("更新成功!");
}
client.close();
}
}
当然,这个 JSON 字符串也可以通过 Map 或者 XContentBuilder 来构建:
Map:
public class UpdateDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
UpdateRequest request = new UpdateRequest("book", "1");
Map docMap = new HashMap();
docMap.put("name", "三国演义888");
request.doc(docMap);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
System.out.println("response.getVersion() = " + response.getVersion());
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("更新成功!");
}
client.close();
}
}
XContentBuilder:
public class UpdateDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
UpdateRequest request = new UpdateRequest("book", "1");
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
jsonBuilder.startObject();
jsonBuilder.field("name", "三国演义666");
jsonBuilder.endObject();
request.doc(jsonBuilder);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
System.out.println("response.getVersion() = " + response.getVersion());
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("更新成功!");
}
client.close();
}
}
也可以通过 upsert 方法实现文档不存在时就添加文档:
public class UpdateDoc {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http"),
new HttpHost("localhost", 9202, "http")
));
UpdateRequest request = new UpdateRequest("book", "99");
XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
jsonBuilder.startObject();
jsonBuilder.field("name", "三国演义666");
jsonBuilder.endObject();
request.doc(jsonBuilder);
request.upsert("{\"name\": \"红楼梦\",\"author\": \"曹雪芹\"}", XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("response.getId() = " + response.getId());
System.out.println("response.getIndex() = " + response.getIndex());
System.out.println("response.getVersion() = " + response.getVersion());
if (response.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("更新成功!");
} else if (response.getResult() == DocWriteResponse.Result.CREATED) {
System.out.println("文档添加成功");
}
client.close();
}
}
ElasticSearch 基础知识: