elasticsearch6实战教程学习笔记(十) — idea + Java实战 3 — 简单查询

上篇介绍了文档的增删改,代码前后内容都一样,只有中间的关键语句不一样:
elasticsearch6实战教程学习笔记(九) — idea + Java实战 2 — 文档的增删改
本篇是之前介绍过的简单查询语句的集合,因为代码的结构都是:启动客户端+连接es+操作语句+输出结果/操作状态+关闭客户端。所以中间的关键语句用注释的方式集中在一起介绍,方便大家对比学习。


话不多说,查询语句的代码如下:

    @Test
    public void esQuery() throws UnknownHostException {
        // 指定es集群;查看 elasticsearch.yml -- put("cluster.name",集群名称)
        Settings settings = Settings.builder().put("cluster.name","elastricsearch").build();

        // 创建访问es服务器的客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddresses(new TransportAddress(InetAddress.getByName("192.168.180.235"),9300));

//        (1) match 查询
        QueryBuilder builder = QueryBuilders.matchQuery("interests","sing");

//        (2) match_all - 查询所有
//        QueryBuilder builder = QueryBuilders.matchAllQuery();

//        (3) multi match:查询词 + 多个字段名(可选
//        QueryBuilder builder = QueryBuilders.multiMatchQuery("abbey","interests","name");

//        (4)term 查询
//        QueryBuilder builder = QueryBuilders.termQuery("interests","sing");

//        (5) terms 查询
//        QueryBuilder builder = QueryBuilders.termsQuery("interests","sing","draw");

//        (6) range - 范围查询。查询日期时可添加 .format("yyyy-MM-dd"):age在20-25的doc
//        QueryBuilder builder = QueryBuilders.rangeQuery("age").from(20).to(25);

//        (7) prefix - 前缀匹配查询:前缀为a的doc
//        QueryBuilder builder = QueryBuilders.prefixQuery("name","a");

//        (8) wildcard:name中有a的doc
//        QueryBuilder builder = QueryBuilders.wildcardQuery("name","*a*");

//        (9) fuzzy - 模糊查询:与alla详细的名字
//        QueryBuilder builder = QueryBuilders.fuzzyQuery("name","alla");

//        (10) type - 根据类型查询:类型为user的所有doc
//        QueryBuilder builder = QueryBuilders.typeQuery("user");

//        (11) ids - 根据id查询:id为1、3的doc
//        QueryBuilder builder = QueryBuilders.idsQuery().addIds("1","3");

//        (12) common
//        QueryBuilder builder = QueryBuilders.commonTermsQuery("name","abbey");

//        (13) query String:精确查询包含sing且不包含draw的。
//        QueryBuilder builder = QueryBuilders.queryStringQuery("+sing -draw");

//        (14) query String:非精确查询包含sing且不包含draw的。
//        QueryBuilder builder = QueryBuilders.simpleQueryStringQuery("+sing -draw");

//       (15)bool:组合查询
//        QueryBuilder builder = QueryBuilders.boolQuery()
//                                .must(QueryBuilders.matchQuery("interests","sing"))
//                                .mustNot(QueryBuilders.matchQuery("interests","draw"))
//                                .should(QueryBuilders.matchQuery("interests","play ball"))
//                                .filter(QueryBuilders.rangeQuery("age").gte(20));

//        (16)constantScore:不计算相关度分数的查询,使用的是常量。
//        QueryBuilder builder = QueryBuilders.constantScoreQuery(QueryBuilders.matchQuery("name","abbey"));

        // 从索引mauanx中查询查询语句为builder,返回doc数量为3。
        SearchResponse response = client.prepareSearch("mauanx")
                .setQuery(builder)
                .setSize(3).get();

        // 获取hits
        SearchHits hits = response.getHits();

        // 输出结果
        System.out.println("查询结果:");
        for(SearchHit h:hits){
            System.out.println(h.getSourceAsString());
            // 将hit转化为map
            Map<String, Object> map = h.getSourceAsMap();
            for(String key:map.keySet()){
                System.out.println("    " + key + " = " + map.get(key));
            }
        }
        // 关闭客户端
        client.close();
    }

使用的索引是之前创建的mauanx,详见elasticsearch6实战教程学习笔记(一) – 基础 中的小实例。这里我使用kibana添加的,大家也可以试着用java添加,具体为:

POST /mauanx/user/_bulk
{"create":{"_index":"mauanx","_type":"user","_id":1}}
{"name":"Abbey","age":20,"sex":"female","interests":["sing","draw"]}
{"create":{"_index":"mauanx","_type":"user","_id":2}}
{"name":"Barbara","age":21,"sex":"female","interests":["climb trees","draw"]}
{"create":{"_index":"mauanx","_type":"user","_id":3}}
{"name":"Cindy","age":22,"sex":"female","interests":["sleep","draw"]}
{"create":{"_index":"mauanx","_type":"user","_id":4}}
{"name":"Daisy","age":19,"sex":"female","interests":["play games","draw"]}
{"create":{"_index":"mauanx","_type":"user","_id":5}}
{"name":"Ella","age":20,"sex":"female","interests":["sing","cook"]}
{"create":{"_index":"mauanx","_type":"user","_id":6}}
{"name":"Fitch","age":23,"sex":"male","interests":["sing","play ball"]}

另外,我又添加了前面提到的id=7文档。

上述查询的部分截图我就不贴出来了。

其中,CommonTerms视频没有讲解,这里随便放个链接参考:
https://blog.csdn.net/ctwy291314/article/details/82836514

你可能感兴趣的:(elasticsearch)