1、删除index1中,title包含工厂的文档
@Test public void testQueryDelete() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //删除index1中,title包含工厂的文档 BulkByScrollResponse response = DeleteByQueryAction.INSTANCE .newRequestBuilder(client) .filter(QueryBuilders.matchQuery("title","工厂")) .source("index1") .get(); //返回删除的个数 long count = response.getDeleted(); System.out.println(count); client.close(); }
2、查询index1下所有文档,只显示3条
@Test public void testMatchAll() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300)); //创建查询对象 QueryBuilder qb = QueryBuilders.matchAllQuery(); //执行查询 SearchResponse sr = client.prepareSearch("index1") .setQuery(qb) .setSize(3)//查询出3个 .get(); //获取结果 SearchHits hits = sr.getHits(); for(SearchHit hit:hits) { //输出json System.out.println(hit.getSourceAsString()); //输出json的key与value Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key+"="+map.get(key)); } } client.close(); }
原文地址:https://www.cnblogs.com/javasl/p/12070422.html
时间: 2024-10-10 10:00:07