• java操作es集群模糊查询等


    首先引入依赖

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.17.13</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    这个依赖中已经包含了一下这些依赖,我们只需要在项目中引入这个依赖即可。
    在这里插入图片描述

    配置

    es使用的是集群配置。引入上面的依赖后我们只需要在springboot项目当中添加以下的配置即可。

    spring:
      data:
        elasticsearch:
          repositories:
            enabled: true
      elasticsearch:
        username: your username
        password: your password word
        uris: http://ip1:port1,http://ip2:port2,http://ip3:port3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用java操作es

    首先引入bean。注意后面导包的时候要用org下面的包而不是co下面的。

    import org.elasticsearch.client.RestHighLevelClient;
    @Autowired
    RestHighLevelClient client;
    
    • 1
    • 2
    • 3
    • 判断某一个索引是否存在
    public boolean existIndex(String index) {
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        boolean exists = false;
        try {
            exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(exists);
        return exists;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    根据id查询文档信息

    public String getDocument(String indexName,long id) {
       GetRequest getRequest = new GetRequest(indexName, id+"");
        try {
            GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
            Map<String, Object> source = documentFields.getSource();
            System.out.println(source);
            return JSON.toJSONString(documentFields);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    批量查询文档信息

    public SearchHits getBatchDocument(String indexName, int size) {
       SearchRequest searchRequest = new SearchRequest(indexName);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.size(size);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            return searchResponse.getHits();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 根据字段值来检索(不是精确匹配)
    public SearchHits getMatchDocument(String indexName, String field, String content) {
        SearchRequest searchRequest = new SearchRequest(indexName);
    
        SearchSourceBuilder builder = new SearchSourceBuilder();
        //这里这个条件只能加一个,再加一个会覆盖前面的内容
        builder.query(QueryBuilders.matchQuery(field, content));
        builder.size(10);
        searchRequest.source(builder);
        try {
            SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
            return search.getHits();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 部分字段精确查询,部分模糊查询,部分范围查询。同时进行分页
    public SearchHits getMatchesDocument(String indexName, Map<String, String> map) {
       SearchRequest request = new SearchRequest(indexName);
        SearchSourceBuilder builder = new SearchSourceBuilder();
    
    
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //逻辑删除标志必须是false,精确查询
        boolQuery.must(QueryBuilders.termQuery("del_flag", "false"));
    
        //按照时间范围查询
        String beginTime = map.remove("beginTime");
        String endTime = map.remove("endTime");
        boolQuery.must(QueryBuilders.rangeQuery("create_time").gte(beginTime).lte(endTime));
    
        //分页查询
        int pageNo = Integer.parseInt(map.remove("pageNo"));
        int pageSize = Integer.parseInt(map.remove("pageSize"));
        builder.from((pageNo-1)*pageSize);
        builder.size(pageSize);
        map.forEach((k, v) -> {
            //按照字段模糊查询
            boolQuery.must(QueryBuilders.matchPhraseQuery(k, v));
        });
        builder.query(boolQuery);
        request.source(builder);
        try {
            SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
            return searchResponse.getHits();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    我们查询的结果中有"_score" : 2.8526313这一项,分数用于判断模糊查询的匹配程度。分数越高匹配程度越高。不需要的话可以查询的时候取消评分降低性能消耗。

    • 对多个字段进行查询,类似于实现sql里面的where id in (******) and delFlag=*的效果

    注意,es根据字段多个值进行查询的时候,需要加上keyword表示不进行分词处理,用于关键词搜索,不然会出现异常的情况,查询到的结果很奇怪。用法如下:

    //根据字段值进行集合查询
    boolQuery.must(QueryBuilders.termsQuery(fieldName+".keyword", values));
    //逻辑删除标志必须是false,精确查询
    boolQuery.must(QueryBuilders.termQuery("del_flag", "false"));
    
    • 1
    • 2
    • 3
    • 4

    总结一下涉及到的查询:

    • 词条查询:term查询,类似=
    • 多值查询:tearms查询,类似in。 ElasticSearch 5.0以后,string类型有重大变更,移除了string类型,string字段被拆分成两种新的数据类型: text用于全文搜索的,而keyword用于关键词搜索。
    • 模糊查询:withcard,类似like。里面?单表单个字符,*代表多个字符
    • 范围查询:range
    • 匹配查询:match,单个字段匹配一个值。
    • 匹配查询:multiMatch,多个字段匹配一个值。
    • 复合查询:boolmust必须满足条件。must_not必须不满足。
      should类似于orfilter关闭评分,提高查询效率。

    附上官方文档

    参考文章1
    参考文章2
    参考文章3
    参考文章4

  • 相关阅读:
    CTF/AWD竞赛标准参考书+实战指南:《AWD特训营》
    高防服务器有用么?
    css-实现卡牌的发牌和翻转动画
    【C语言】每日一题(杨氏矩阵查找数)
    IDEA闪退解决方案记录
    Django 表单处理:从前端到后台的全流程指南
    Go fsnotify简介
    【Go】Go语言中的数组基本语法与应用实战
    【算法03】对数器
    GitHub上线重量级分布式架构原理设计笔记,开源的东西看着就是爽
  • 原文地址:https://blog.csdn.net/qq_45401910/article/details/132823222