码迷,mamicode.com
首页 > 编程语言 > 详细

(21)ElasticSearch java项目中match、multimath、term、terms以及范围、前缀、通配符、模糊、类型、ids等查询示例

时间:2019-12-23 00:37:12      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:san   research   add   ons   json   tin   cut   rup   user   

  1、查询index1中content字段包含工厂的文档

@Test
    public void testMatch() 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.matchQuery("content","工厂");
        //执行查询
        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();
   }

  2、查询index1中title字段或者content字段包含设计的文档

@Test
    public void testMultiMatch() 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));
        //创建查询对象,查询title或者content包含设计的
        QueryBuilder qb = QueryBuilders.multiMatchQuery("设计","title","content");
        //执行查询
        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();
   }

  3、查询index1中title字段包含设计的

@Test
    public void testTermQuery() 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));
        //创建查询对象,查询title包含设计的
        QueryBuilder qb = QueryBuilders.termQuery("title","设计");
        //执行查询
        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();
   }

  4、在index1中查询title字段包含设计或者静态工厂的

@Test
    public void testTermsQuery() 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));
        //创建查询对象,查询title包含设计或者静态工厂的
        QueryBuilder qb = QueryBuilders.termsQuery("title","设计","静态工厂");
        //执行查询
        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();
   }

  5、在index1中查询postdate的范围在2018-05-20到2018-05-21之间的

@Test
    public void testRangeQuery() 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));
        //创建查询对象,查询postdate的范围在2018-05-20到2018-05-21之间的
        QueryBuilder qb = QueryBuilders.rangeQuery("postdate").from("2018-05-20").to("2018-05-21").format("yyyy-MM-dd");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .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();
   }

  6、在index1中查询url字段以csdn开头的文档

@Test
    public void testPrefixQuery() 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));
        //创建查询对象,查询url以csdn开头的文档
        QueryBuilder qb = QueryBuilders.prefixQuery("url","csdn");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .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();
   }

  7、在index1中查询url字段以csdn开头的文档

@Test
    public void testWildcardQuery() 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));
        //创建查询对象,查询url以csdn开头的文档
        QueryBuilder qb = QueryBuilders.wildcardQuery("url","csdn*");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .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();
   }

  8、在index1中模糊查询interests字段中包含changge的文档

@Test
    public void testFuzzyQuery() 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));
        //创建查询对象,查询interests字段中包含changge的文档,("interests":"xi huan tingyinyue,changge,tiaowu")
        QueryBuilder qb = QueryBuilders.fuzzyQuery("interests","chagge");//即使changge写成chagge也能模糊查询出来
        //执行查询
        SearchResponse sr = client.prepareSearch("lib3")
                                  .setQuery(qb)
                                  .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();
   }

  9、在index1中查询类型是blog的文档

@Test
    public void testTypeQuery() 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));
        //创建查询对象,查询类型是blog的
        QueryBuilder qb = QueryBuilders.typeQuery("blog");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")//索引是index1的
                                  .setQuery(qb)
                                  .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();
   }

  10、在索引lib3中查询name是zhangsan的文档(类似于GET lib3/user/_search?q=name:zhangsan)

@Test
    public void testCommonTermsQuery() 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));
        //创建查询对象,查询name是zhangsan的
        QueryBuilder qb = QueryBuilders.commonTermsQuery("name","zhangsan");
        //执行查询
        SearchResponse sr = client.prepareSearch("lib3")
                                  .setQuery(qb)
                                  .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();
   }

  11、在索引lib3中查询name字段是zhangsan的文档(不计算分数查询) 

@Test
    public void testConstantScoreQuery() 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));
        //创建查询对象,查询name字段是zhangsan的文档,不计算分数查询
        QueryBuilder qb = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name","zhangsan"));
        //执行查询
        SearchResponse sr = client.prepareSearch("lib3")
                                  .setQuery(qb)
                                  .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();
   }

  12、在index1中查询id是4、8的文档

@Test
    public void testIdsQuery() 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));
        //创建查询对象,查询id是4、8的
        QueryBuilder qb = QueryBuilders.idsQuery().addIds("4","8");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")//索引是index1
                                  .setQuery(qb)
                                  .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();
   }

(21)ElasticSearch java项目中match、multimath、term、terms以及范围、前缀、通配符、模糊、类型、ids等查询示例

标签:san   research   add   ons   json   tin   cut   rup   user   

原文地址:https://www.cnblogs.com/javasl/p/12070427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!