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

不管是同步回调还是异步回调,如果调用成功,都会返回 IndexRespose 对象。 这个对象中包含什么信息呢?看下面代码

String index = indexResponse.getIndex(); String type = indexResponse.getType(); String id = indexResponse.getId(); long version = indexResponse.getVersion(); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { // 文档第一次创建 } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { // 文档之前已存在,当前是重写 } ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { // 成功的分片数量少于总分片数量 } if (shardInfo.getFailed() > 0) { for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { String reason = failure.reason(); // 处理潜在的失败信息 } }

在索引时有版本冲突的话,会抛出 ElasticsearchException

IndexRequest request = new IndexRequest("posts", "doc", "1") .source("field", "value") .version(1); // 这里是文档版本号 try { IndexResponse response = client.index(request); } catch(ElasticsearchException e) { if (e.status() == RestStatus.CONFLICT) { // 冲突了 } }

如果将 opType 设置为 create, 而且如果索引的文档与已存在的文档在 index, type 和 id 上均相同,也会抛出冲突异常。

IndexRequest request = new IndexRequest("posts", "doc", "1") .source("field", "value") .opType(DocWriteRequest.OpType.CREATE); try { IndexResponse response = client.index(request); } catch(ElasticsearchException e) { if (e.status() == RestStatus.CONFLICT) { } } GET API GET 请求

每个 GET 请求都必须需传入下面 3 个参数

Index

Type

Document id

GetRequest getRequest = new GetRequest( "posts", "doc", "1"); 可选参数

下面的参数都是可选的, 里面的选项并不完整,如要获取完整的属性,请参考 官方文档

不获取源数据,默认是获取的

request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);

配置返回数据中包含指定字段

String[] includes = new String[]{"message", "*Date"}; String[] excludes = Strings.EMPTY_ARRAY; FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); request.fetchSourceContext(fetchSourceContext);

配置返回数据中排除指定字段

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

实时 默认为 true

request.realtime(false);

版本

request.version(2);

版本类型

request.versionType(VersionType.EXTERNAL); 同步执行 GetResponse getResponse = client.get(getRequest); 异步执行

此部分与 index 相似, 只有一点不同, 返回类型为 GetResponse

代码部分略

Get Response

返回的 GetResponse 对象包含要请求的文档数据(包含元数据和字段)

String index = getResponse.getIndex(); String type = getResponse.getType(); String id = getResponse.getId(); if (getResponse.isExists()) { long version = getResponse.getVersion(); String sourceAsString = getResponse.getSourceAsString(); // string 形式 Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); // map byte[] sourceAsBytes = getResponse.getSourceAsBytes(); // 字节形式 } else { // 没有发现请求的文档 }

在请求中如果包含特定的文档版本,如果与已存在的文档版本不匹配, 就会出现冲突

try { GetRequest request = new GetRequest("posts", "doc", "1").version(2); GetResponse getResponse = client.get(request); } catch (ElasticsearchException exception) { if (exception.status() == RestStatus.CONFLICT) { // 版本冲突 } } Exists API

如果文档存在 Exists API 返回 true, 否则返回 fasle。

Exists Request

GetRequest 用法和 差不多,两个对象的可选参数是相同的。由于 exists() 方法只返回 true 或者 false, 建议将获取 _source 以及任何存储字段的值关闭,尽量使请求轻量级。

GetRequest getRequest = new GetRequest( "posts", // Index "doc", // Type "1"); // Document id getRequest.fetchSourceContext(new FetchSourceContext(false)); // 禁用 _source 字段 getRequest.storedFields("_none_"); // 禁止存储任何字段 同步请求 boolean exists = client.exists(getRequest); 异步请求

异步请求与 Index API 相似,此处不赘述,只粘贴代码。如需详细了解,请参阅

ActionListener<Boolean> listener = new ActionListener<Boolean>() { @Override public void onResponse(Boolean exists) { } @Override public void onFailure(Exception e) { } }; client.existsAsync(getRequest, listener); Delete API

官方地址

Delete Request

DeleteRequest 必须传入下面参数

DeleteRequest request = new DeleteRequest( "posts", // index "doc", // doc "1"); // document id 可选参数

超时时间

request.timeout(TimeValue.timeValueMinutes(2)); request.timeout("2m");

刷新策略

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

版本

request.version(2);

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

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