• SpringBoot(一)集成 elasticsearch:7.11.2


    依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4

    配置

    spring: 
      elasticsearch:
        rest:
          uris: 192.168.1.101:9200
    
    • 1
    • 2
    • 3
    • 4

    多条件查询

    
    
    import cn.hutool.json.JSONUtil;
    import com.google.common.collect.Lists;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.lucene.search.TotalHits;
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.index.query.RangeQueryBuilder;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.sort.FieldSortBuilder;
    import org.elasticsearch.search.sort.SortOrder;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    import java.util.Objects;
    
    /**
     * @author Wang
     * @Date 2022/8/27
     */
    @Service
    @Slf4j
    @RequiredArgsConstructor
    public class IotDataLogServiceImpl implements IotDataLogService {
    
        /**
         * ① ElasticsearchRestTemple是ElasticsearchOperations的子类的子类
         * ② 在ES7.x以下的版本使用的是ElasticsearchTemple,7.x以上版本已弃用ElasticsearchTemple,使用ElasticsearchRestTemple替代
         */
        final RestHighLevelClient highLevelClient;
    
        private static final String IOT_DATA_LOG = "iot_data_log";
        private static final String IOT_SIMULATOR_LOG = "iot_simulator_log";
    
    
        @Override
        public TableDataInfo searchV2(IotDataLogRequest iotDataLogRequest) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            SearchRequest searchRequest = new SearchRequest(IOT_DATA_LOG);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
            if (StringUtils.isNotEmpty(iotDataLogRequest.getDeviceUid())) {
                boolBuilder.must(QueryBuilders.termQuery("deviceUid", iotDataLogRequest.getDeviceUid()));
            }
            if (StringUtils.isNotEmpty(iotDataLogRequest.getProductUid())) {
                boolBuilder.must(QueryBuilders.termQuery("productUid", iotDataLogRequest.getProductUid()));
            }
            if (StringUtils.isNotEmpty(iotDataLogRequest.getTopic())) {
                boolBuilder.must(QueryBuilders.termQuery("topic", iotDataLogRequest.getTopic()));
            }
            // createTime > start and createTime < end
            if (Objects.nonNull(iotDataLogRequest.getStart()) && Objects.nonNull(iotDataLogRequest.getEnd())) {
                RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("createTime")
                        .from(iotDataLogRequest.getStart())
                        .to(iotDataLogRequest.getEnd());
                boolBuilder.must(rangeQueryBuilder);
            }
            sourceBuilder.from(iotDataLogRequest.getPageNum() * iotDataLogRequest.getPageSize());
            sourceBuilder.size(iotDataLogRequest.getPageSize());
            sourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));
            sourceBuilder.query(boolBuilder);
            searchRequest.source(sourceBuilder);
    
            try {
                SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
                org.elasticsearch.search.SearchHits hits = Objects.requireNonNull(response).getHits();
                TotalHits totalHits = hits.getTotalHits();
                long total = Objects.nonNull(totalHits) ? totalHits.value : 0;
                List<IotDataLog> list = Lists.newArrayList();
                for (org.elasticsearch.search.SearchHit hit : hits.getHits()) {
                    IotDataLog data = JSONUtil.toBean(hit.getSourceAsString(), IotDataLog.class);
                    list.add(data);
                }
                tableDataInfo.setRows(list);
                tableDataInfo.setTotal(total);
                tableDataInfo.setCode(HttpStatus.OK.value());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return tableDataInfo;
        }
    
        @Override
        public TableDataInfo searchSimulator(IotDataLogRequest iotDataLogRequest) {
            TableDataInfo tableDataInfo = new TableDataInfo();
            SearchRequest searchRequest = new SearchRequest(IOT_SIMULATOR_LOG);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
            // and deviceUid=deviceUid
            if (StringUtils.isNotEmpty(iotDataLogRequest.getDeviceUid())) {
                boolBuilder.must(QueryBuilders.termQuery("deviceUid", iotDataLogRequest.getDeviceUid()));
            }
            // and simulatorUid=simulatorUid
            if (StringUtils.isNotEmpty(iotDataLogRequest.getSimulatorUid())) {
                boolBuilder.must(QueryBuilders.termQuery("simulatorUid", iotDataLogRequest.getSimulatorUid()));
            }
            // and topic=topic
            if (StringUtils.isNotEmpty(iotDataLogRequest.getTopic())) {
                boolBuilder.must(QueryBuilders.termQuery("topic", iotDataLogRequest.getTopic()));
            }
            // and (source = source or to = to)
            if (StringUtils.isNotEmpty(iotDataLogRequest.getSource()) && StringUtils.isNotEmpty(iotDataLogRequest.getTo())) {
                boolBuilder.should(QueryBuilders.matchQuery("source", iotDataLogRequest.getSource()))
                        .should(QueryBuilders.matchQuery("to", iotDataLogRequest.getTo()));
                // 至少匹配一个条件
                boolBuilder.minimumShouldMatch(1);
            }
            sourceBuilder.from(iotDataLogRequest.getPageNum() * iotDataLogRequest.getPageSize());
            sourceBuilder.size(iotDataLogRequest.getPageSize());
            sourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));
            sourceBuilder.query(boolBuilder);
            searchRequest.source(sourceBuilder);
    
            try {
                SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
                log.debug(searchRequest.source().toString());
                org.elasticsearch.search.SearchHits hits = Objects.requireNonNull(response).getHits();
                TotalHits totalHits = hits.getTotalHits();
                long total = Objects.nonNull(totalHits) ? totalHits.value : 0;
                List<IotSimulatorLog> list = Lists.newArrayList();
                for (org.elasticsearch.search.SearchHit hit : hits.getHits()) {
                    IotSimulatorLog data = JSONUtil.toBean(hit.getSourceAsString(), IotSimulatorLog.class);
                    list.add(data);
                }
                tableDataInfo.setRows(list);
                tableDataInfo.setTotal(total);
                tableDataInfo.setCode(HttpStatus.OK.value());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return tableDataInfo;
    
        }
    
    }
    
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144

    拓展学习

    等值查询

    sql

    select * from person where name = '张无忌';
    
    • 1

    es api

    @Autowired
    private RestHighLevelClient client;
    
    @Test
    public void queryTerm() throws IOException {
     // 根据索引创建查询请求
        SearchRequest searchRequest = new SearchRequest("person");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 构建查询语句
        searchSourceBuilder.query(QueryBuilders.termQuery("name.keyword", "张无忌"));
        System.out.println("searchSourceBuilder=====================" + searchSourceBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(JSONObject.toJSON(response));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    多值查询-terms

    sql

    select * from persons where sect in('明教','武当派');
    
    • 1

    es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.termsQuery("sect.keyword", Arrays.asList("明教", "武当派")));
    
    • 1
    • 2
    • 3

    范围查询-range

    sql

    select * from pesons where age between 18 and 22;
    
    • 1

    es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.rangeQuery("age").gte(10).lte(30));
    
    • 1
    • 2
    • 3

    前缀查询-prefix

    sql

    select * from persons where sect like '武当%';
    
    • 1

    es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.prefixQuery("sect.keyword","武当"));
    
    • 1
    • 2
    • 3

    通配符查询-wildcard

    sql

    select * from persons where name like '张%忌';
    
    • 1

    es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.wildcardQuery("sect.keyword","张*忌"));
    
    • 1
    • 2
    • 3

    复合查询

    sql

    select * from persons where sex = '女' and sect = '明教';
    
    • 1

    es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("sex", "女"))
            .must(QueryBuilders.termQuery("sect.keyword", "明教"))
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    布尔查询

    精度控制:

    所有 must 语句必须匹配,所有 must_not 语句都必须不匹配,但有多少 should 语句应该匹配呢?默认情况下,没有 should 语句是必须匹配的,只有一个例外:那就是当没有 must 语句的时候,至少有一个 should 语句必须匹配。
    我们可以通过 minimum_should_match 参数控制需要匹配的 should 语句的数量,它既可以是一个绝对的数字,又可以是个百分比

    简单实现

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("sex", "女"))
            .should(QueryBuilders.termQuery("address.word", "峨眉山"))
            .should(QueryBuilders.termQuery("sect.keyword", "明教"))
            .minimumShouldMatch(1)
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    复杂sql

    select 
     *
    from
     persons
    where 
     sex = '女'
    and
     age between 30 and 40
    and 
     sect != '明教'
    and 
     (address = '峨眉山' OR skill = '暗器')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    复杂 es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("sex", "女"))
            .must(QueryBuilders.rangeQuery("age").gte(30).lte(40))
            .mustNot(QueryBuilders.termQuery("sect.keyword", "明教"))
            .should(QueryBuilders.termQuery("address.keyword", "峨眉山"))
            .should(QueryBuilders.rangeQuery("power.keyword").gte(50).lte(80))
            .minimumShouldMatch(1);  // 设置should至少需要满足几个条件
    
    // 将BoolQueryBuilder构建到SearchSourceBuilder中
    searchSourceBuilder.query(boolQueryBuilder);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    filter查询

    query和filter的区别:query查询的时候,会先比较查询条件,然后计算分值,最后返回文档结果;而filter是先判断是否满足查询条件,如果不满足会缓存查询结果(记录该文档不满足结果),满足的话,就直接缓存结果,filter不会对结果进行评分,能够提高查询效率。
    filter的使用方式比较多样,下面用几个例子演示一下。

    简单实现

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.boolQuery()
            .filter(QueryBuilders.termQuery("sex", "男"))
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5

    方式二,和must、must_not同级,相当于子查询
    sql

    select * from (select * from persons where sect = '明教')) a where sex = '女';
    
    • 1

    es api

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.boolQuery()
            .must(QueryBuilders.termQuery("sect.keyword", "明教"))
            .filter(QueryBuilders.termQuery("sex", "女"))
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    方式三,将must、must_not置于filter下,这种方式是最常用的:

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    // 构建查询语句
    searchSourceBuilder.query(QueryBuilders.boolQuery()
            .filter(QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("sect.keyword", "明教"))
                    .must(QueryBuilders.rangeQuery("age").gte(20).lte(35))
                    .mustNot(QueryBuilders.termQuery("sex.keyword", "女")))
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    参考链接
    https://z.itpub.net/article/detail/A7B79869961FB96969AADEA98959D9FC

  • 相关阅读:
    TCP机制之确认应答及超时重传
    RT-Thread学习笔记(三):线程管理
    含文档+PPT+源码等]精品基于SpringCloud实现的分布式电影-微服务毕业设计项目源码-分布式毕设项目[包运行成功]
    使用del语句删除名称对引用计数的影响
    聊跨境:一年增长60%!人人都说跨境电商难做,但红利仍在(Starday)
    C++学习Day3:面向对象OOP、抽象
    请求模块(requests)
    阿里P7程序员斩获offer感言:突破Java瓶颈期我用了这一套路线图
    Flink的简单学习(kafka)三
    【漏洞复现-spring-目录遍历】vulfocus/spring-cve_2020_5410
  • 原文地址:https://blog.csdn.net/SpringHASh/article/details/126597210