java操作elasticsearch实现条件查询(match、multiMatch、term、terms、reange)

1、条件match query查询

//条件查询match query
    @Test
    public void test10() 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 builder = QueryBuilders.matchQuery("interests", "changge");
                SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
                SearchHits hits = response.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));
                    }
                }
    }

2、multimatch query查询

     说明:查询的值在多个字段中进行匹配查询

//multiMatchQuery 查询的值在多个字段中进行匹配
    @Test
    public void test11() 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 builder = QueryBuilders.multiMatchQuery("changge", "address","interests");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.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));
            }
        }
    }

3、term查询

  说明:term查询是不支持分词查询的

//term查询
    @Test
    public void test12() 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));
        //term查询是不进行分词的
        QueryBuilder builder = QueryBuilders.termQuery("interests", "changge");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.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));
            }
        }
    }

term中文精确查询注意点

  当使用term查询中文时,比如查询"人类",我们是查不出来的,因为字段设置了ik分词器,他会将“人类”分成“人”和“类”,所有针对这种情况,我们需要将该字段类型临时转成keword类型,转换成后当字段中有"人类很好"的文章,我们也查询不出来,只会查询出字段为人类的文章。如下:

//term中文查询
    @Test
    public void test12() 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));
        //term查询是不进行分词的
        QueryBuilder builder = QueryBuilders.termQuery("interests.keword", "人类");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.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));
            }
        }
    }

 

4、terms查询

  说明:与term的区别在于terms可以同时匹配多个条件

//terms查询:与term区别在于可以在同个字段中同时匹配多个条件,但是不支持分词
    public void test13() 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));
        //terms查询是不进行分词的 与term的区别在terms可以指定一个字段匹配多个查询内容
        QueryBuilder builder = QueryBuilders.termsQuery("interests", "changge","旅游");
        SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
        SearchHits hits = response.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));
            }
        }
    }

5、reange 范围查询

//reange 范围查询(日期在多少之间等)
        @Test
        public void test14() 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));
            //rangeQurey 第一个参数为字段名,后面是范围 在设置日期格式
            QueryBuilder builder = QueryBuilders.rangeQuery("birthday").from("1990-01-01").to("2000-10-10").format("yyyy-MM-dd");
            SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get();
            SearchHits hits = response.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实现前缀查询、wildcard模糊查询、fuzzy模糊查询、ids查询。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/

技术在于交流!

 

posted @ 2019-01-21 00:49  陈远波  阅读(16465)  评论(1编辑  收藏  举报