固安建站公司,自己的网站,建站 网站程序有哪些,网络维护工程师是做什么的背景
在Elasticsearch中#xff0c;delete_by_query API 允许你基于查询条件删除文档。在Java中#xff0c;你可以使用Elasticsearch的Rest High Level Client或者Transport Client来执行这个操作。
示例代码
下面是使用Rest High Level Client进行delete_by_query操作的一…背景
在Elasticsearch中delete_by_query API 允许你基于查询条件删除文档。在Java中你可以使用Elasticsearch的Rest High Level Client或者Transport Client来执行这个操作。
示例代码
下面是使用Rest High Level Client进行delete_by_query操作的一个示例代码。
首先你需要添加Elasticsearch的依赖到你的项目中。如果你使用Maven可以添加如下依赖到你的pom.xml文件中
dependencygroupIdorg.elasticsearch.client/groupIdartifactIdelasticsearch-rest-high-level-client/artifactIdversion7.10.1/version
/dependency确保版本与你的Elasticsearch集群版本相匹配。
下面是使用Rest High Level Client执行delete_by_query操作的Java代码示例
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;import java.io.IOException;public class DeleteByQueryExample {public static void main(String[] args) throws IOException {// 创建Elasticsearch客户端try (RestHighLevelClient client new RestHighLevelClient(RestClient.builder(new HttpHost(localhost, 9200, http)))) {// 创建DeleteByQueryRequestDeleteByQueryRequest request new DeleteByQueryRequest(your_index); // 替换为你的索引名request.setQuery(QueryBuilders.matchQuery(field, value)); // 替换为你的查询条件// 执行delete_by_query操作BulkByScrollResponse response client.deleteByQuery(request, RequestOptions.DEFAULT);// 输出结果System.out.println(Deleted documents: response.getDeleted());} catch (Exception e) {e.printStackTrace();}}
}在上面的代码中我们首先创建了一个RestHighLevelClient实例用于与Elasticsearch集群通信。接着我们构建了一个DeleteByQueryRequest对象并设置了索引名和查询条件。然后我们调用client.deleteByQuery方法来执行删除操作并通过BulkByScrollResponse对象获取操作结果。
请注意你需要根据你的具体需求替换your_index和查询条件。另外HttpHost的参数应该与你的Elasticsearch集群配置相匹配。
在使用之前请确保你的Elasticsearch集群版本与客户端库版本兼容并且你已经正确配置了Elasticsearch的连接信息。
设置更新结果对搜索可见
为了确保delete_by_query操作后的结果能够立即对搜索可见你可以设置RefreshPolicy为IMMEDIATE。这将导致操作完成后立即刷新索引。以下是在上面示例代码的基础上设置RefreshPolicy的方法
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.apache.http.HttpHost;import java.io.IOException;public class DeleteByQueryExample {public static void main(String[] args) throws IOException {// 创建Elasticsearch客户端try (RestHighLevelClient client new RestHighLevelClient(RestClient.builder(new HttpHost(localhost, 9200, http)))) {// 创建DeleteByQueryRequestDeleteByQueryRequest request new DeleteByQueryRequest(your_index); // 替换为你的索引名request.setQuery(QueryBuilders.matchQuery(field, value)); // 替换为你的查询条件// 设置RefreshPolicy为IMMEDIATErequest.setRefresh(true); // 这是简写方式// 或者更明确地使用下面的方法// request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);// 执行delete_by_query操作BulkByScrollResponse response client.deleteByQuery(request, RequestOptions.DEFAULT);// 输出结果System.out.println(Deleted documents: response.getDeleted());} catch (Exception e) {e.printStackTrace();}}
}在DeleteByQueryRequest对象上调用setRefresh(true)或setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)会将刷新策略设置为立即刷新。这意味着一旦delete_by_query操作完成Elasticsearch会立即刷新相关的分片使得删除的结果可以立即被搜索到。
请注意频繁地使用IMMEDIATE刷新策略可能会对性能产生负面影响因为每次刷新都是一个相对昂贵的操作。因此它应该谨慎使用特别是在高吞吐量的生产环境中。
结尾
希望以上示例代码能够帮助你在Java中使用Elasticsearch的Rest High Level Client执行delete_by_query操作。另外谨慎使用立即刷新策略特别是在高吞吐量的生产环境中。
祝你在使用Elasticsearch时顺利无阻 文章转载自: http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.bbtn.cn.gov.cn.bbtn.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.rfzbm.cn.gov.cn.rfzbm.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.qqnp.cn.gov.cn.qqnp.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.ghslr.cn.gov.cn.ghslr.cn http://www.morning.sfgzx.cn.gov.cn.sfgzx.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.niukaji.com.gov.cn.niukaji.com http://www.morning.gprzp.cn.gov.cn.gprzp.cn http://www.morning.psgbk.cn.gov.cn.psgbk.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.knryp.cn.gov.cn.knryp.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.tjcgl.cn.gov.cn.tjcgl.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.lwtfx.cn.gov.cn.lwtfx.cn http://www.morning.qggcc.cn.gov.cn.qggcc.cn http://www.morning.bscsp.cn.gov.cn.bscsp.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.jljwk.cn.gov.cn.jljwk.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.rsqpc.cn.gov.cn.rsqpc.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.rybr.cn.gov.cn.rybr.cn http://www.morning.rkkh.cn.gov.cn.rkkh.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.sqqpb.cn.gov.cn.sqqpb.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.bpmdh.cn.gov.cn.bpmdh.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.sacxbs.cn.gov.cn.sacxbs.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.nqrlz.cn.gov.cn.nqrlz.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn