后期博客本人都只给出代码,具体的说明在代码中也有注释。
1、查询删除
//查询删除:将查询到的数据进行删除 @Test public void test8() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); BulkByScrollResponse response = DeleteByQueryAction.INSTANCE .newRequestBuilder(client) .filter(QueryBuilders.matchQuery("title", "工厂")) .source("index1") .get(); //删除并返回删除的个数 long counts =response.getDeleted(); System.out.println(counts); }
2、查询所有(match_all)
//match_all查询所有 @Test public void test9() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); QueryBuilder qBuilder = QueryBuilders.matchAllQuery(); SearchResponse sResponse = client.prepareSearch("lib3") .setQuery(qBuilder) .get(); SearchHits hits = sResponse.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //将获取的值转换成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } }
下一篇博客本人将书写java操作elasticsearch实现部分的条件查询(mutch/multimatch /term/terms/reange)。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/
技术在于交流!