Elasticsearch java api 6.6 Document APIs(3) Exists API

本文深入探讨了ExistsAPI的工作原理,展示了如何使用ExistsRequest检查文档是否存在,包括禁用fetch_source和存储字段以提高请求效率的方法。同时,文章介绍了同步和异步请求的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Exists API

exists API在文档存在时返回true,否则返回false。

Exists Request

它使用GetRequest就像使用Get API一样。它的所有可选参数都受到支持。因为exists()只返回true或false,所以我们建议关闭fetch_source和所有存储字段,这样请求会更轻一些:

GetRequest getRequest = new GetRequest(
    "posts", 
    "doc",   
    "1");    
getRequest.fetchSourceContext(new FetchSourceContext(false));  //Disable fetching _source.
getRequest.storedFields("_none_");   //Disable fetching stored fields.

同步请求

当以如下方式执行GetRequest时,客户端等待返回boolean值,然后继续执行代码:

boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);

异步请求

client.existsAsync(getRequest, RequestOptions.DEFAULT, listener); 

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.6/java-rest-high-document-delete.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值