Elasticsearch Java Rest Client API 整理总结 (一) (3)

版本类型

request.versionType(VersionType.EXTERNAL); 同步执行 DeleteResponse deleteResponse = client.delete(request); 异步执行 ActionListener<DeleteResponse> listener = new ActionListener<DeleteResponse>() { @Override public void onResponse(DeleteResponse deleteResponse) { } @Override public void onFailure(Exception e) { } }; client.deleteAsync(request, listener); Delete Response

DeleteResponse 可以检索执行操作的信息,如代码所示

String index = deleteResponse.getIndex(); String type = deleteResponse.getType(); String id = deleteResponse.getId(); long version = deleteResponse.getVersion(); ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { // 成功分片数目小于总分片 } if (shardInfo.getFailed() > 0) { for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { String reason = failure.reason(); // 处理潜在失败 } }

也可以来检查文档是否存在

DeleteRequest request = new DeleteRequest("posts", "doc", "does_not_exist"); DeleteResponse deleteResponse = client.delete(request); if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) { // 文档不存在 }

版本冲突时也会抛出 `ElasticsearchException

try { DeleteRequest request = new DeleteRequest("posts", "doc", "1").version(2); DeleteResponse deleteResponse = client.delete(request); } catch (ElasticsearchException exception) { if (exception.status() == RestStatus.CONFLICT) { // 版本冲突 } } Update API Update Request

UpdateRequest 的必需参数如下

UpdateRequest request = new UpdateRequest( "posts", // Index "doc", // 类型 "1"); // 文档 Id 使用脚本更新

官方地址

部分文档更新

在更新部分文档时,已存在文档与部分文档会合并。

部分文档可以有以下形式

JSON 格式

UpdateRequest request = new UpdateRequest("posts", "doc", "1"); String jsonString = "{" + "\"updated\":\"2017-01-01\"," + "\"reason\":\"daily update\"" + "}"; request.doc(jsonString, XContentType.JSON);

Map 格式

Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("updated", new Date()); jsonMap.put("reason", "daily update"); UpdateRequest request = new UpdateRequest("posts", "doc", "1") .doc(jsonMap);

XContentBuilder 对象

XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.timeField("updated", new Date()); builder.field("reason", "daily update"); } builder.endObject(); UpdateRequest request = new UpdateRequest("posts", "doc", "1") .doc(builder);

Object key-pairs

UpdateRequest request = new UpdateRequest("posts", "doc", "1") .doc("updated", new Date(), "reason", "daily update"); Upserts

如果文档不存在,可以使用 upserts 方法将文档以新文档的方式创建。

UpdateRequest request = new UpdateRequest("posts", "doc", "1") .doc("updated", new Date(), "reason", "daily update");

upserts 方法支持的文档格式与 update 方法相同。

可选参数

超时时间

request.timeout(TimeValue.timeValueSeconds(1)); request.timeout("1s");

刷新策略

request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); request.setRefreshPolicy("wait_for");

冲突后重试次数

request.retryOnConflict(3);

获取数据源,默认是开启的

request.fetchSource(true);

包括特定字段

String[] includes = new String[]{"updated", "r*"}; String[] excludes = Strings.EMPTY_ARRAY; request.fetchSource(new FetchSourceContext(true, includes, excludes));

排除特定字段

String[] includes = Strings.EMPTY_ARRAY; String[] excludes = new String[]{"updated"}; request.fetchSource(new FetchSourceContext(true, includes, excludes));

指定版本

request.version(2);

禁用 noop detection

request.scriptedUpsert(true);

设置如果更新的文档不存在,就必须要创建一个

request.docAsUpsert(true); 同步执行 UpdateResponse updateResponse = client.update(request); 异步执行

此处只贴代码,官方地址

ActionListener<UpdateResponse> listener = new ActionListener<UpdateResponse>() { @Override public void onResponse(UpdateResponse updateResponse) { } @Override public void onFailure(Exception e) { } }; client.updateAsync(request, listener); Update Response String index = updateResponse.getIndex(); String type = updateResponse.getType(); String id = updateResponse.getId(); long version = updateResponse.getVersion(); if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) { // 文档已创建 } else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) { // 文档已更新 } else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) { // 文档已删除 } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) { // 文档不受更新的影响 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpwpsx.html