ElasticSearch工具类 - ESUtils

1、引入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.15.0</version>
</dependency>

2、es配置

java 复制代码
spring:
  data:
    elasticsearch:
      cluster-nodes: localhost:9200

3、创建ESUtils工具类

java 复制代码
import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ESUtils {
    @Resource
    private RestHighLevelClient elasticsearchClient;

    /**
     * 滚动查询全部数据
     * @param indexName 查询的索引 - 数据库
     */
    public List<String> queryDataInRange(String indexName, SearchSourceBuilder sourceBuilder) {
        List<String> results = new ArrayList<>();
        String scrollId = null;
        try {
            SearchRequest searchRequest = new SearchRequest(indexName)
                    .source(sourceBuilder)
                    .scroll(TimeValue.timeValueMinutes(1));

            // 执行搜索请求,获取第一批数据和 scrollId
            SearchResponse searchResponse = elasticsearchClient.search(searchRequest, RequestOptions.DEFAULT);
            scrollId = searchResponse.getScrollId();
            SearchHit[] searchHits = searchResponse.getHits().getHits();

            // 处理第一批数据
            results.addAll(processHits(searchHits));

            // 滚动查询处理后续的数据
            while (searchHits != null && searchHits.length > 0) {
                SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId)
                        .scroll(TimeValue.timeValueMinutes(1));
                // 滚动查询
                searchResponse = elasticsearchClient.scroll(scrollRequest, RequestOptions.DEFAULT);
                scrollId = searchResponse.getScrollId();
                searchHits = searchResponse.getHits().getHits();

                // 处理后续数据
                results.addAll(processHits(searchHits));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 清除 scroll 上下文
            if (scrollId != null) {
                ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
                clearScrollRequest.addScrollId(scrollId);
                try {
                    elasticsearchClient.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return results;
    }

    // 处理查询到的数据
    private List<String> processHits(SearchHit[] searchHits) {
        List<String> results = new ArrayList<>();
        for (SearchHit hit : searchHits) {
            results.add(hit.getSourceAsString());
        }
        return results;
    }

}

4、测试工具类

java 复制代码
import com.example.es.utils.ESUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
class EsDemoApplicationTests {
    @Resource
    private ESUtils esUtils;

    @Test
    void contextLoads() {
        // 构造初始的搜索请求
        RangeQueryBuilder queryBuilder = QueryBuilders
                .rangeQuery("timeField")
                .gte("2024-03-18 00:00:00")
                .lte("2024-03-18 23:59:59");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .query(queryBuilder)
                .sort("timeField", SortOrder.ASC)
                .size(1000);
        List<String> resultMap = esUtils.queryDataInRange("index-Bank", sourceBuilder);
        resultMap.forEach(System.out::println);
    }

}
相关推荐
Elasticsearch3 小时前
需要知道某个同义词是否实际匹配了你的 Elasticsearch 查询吗?
elasticsearch
SelectDB20 小时前
易车 × Apache Doris:构建湖仓一体新架构,加速 AI 业务融合实践
大数据·agent·mcp
武子康1 天前
大数据-241 离线数仓 - 实战:电商核心交易数据模型与 MySQL 源表设计(订单/商品/品类/店铺/支付)
大数据·后端·mysql
IvanCodes1 天前
一、消息队列理论基础与Kafka架构价值解析
大数据·后端·kafka
武子康2 天前
大数据-240 离线数仓 - 广告业务 Hive ADS 实战:DataX 将 HDFS 分区表导出到 MySQL
大数据·后端·apache hive
洛森唛3 天前
ElasticSearch查询语句Query String详解:从入门到精通
后端·elasticsearch
字节跳动数据平台3 天前
5000 字技术向拆解 | 火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
武子康3 天前
大数据-239 离线数仓 - 广告业务实战:Flume 导入日志到 HDFS,并完成 Hive ODS/DWD 分层加载
大数据·后端·apache hive
洛森唛4 天前
Elasticsearch DSL 查询语法大全:从入门到精通
后端·elasticsearch
字节跳动数据平台4 天前
代码量减少 70%、GPU 利用率达 95%:火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据