1.springboot 集成elasticsearch组件

这篇具有很好参考价值的文章主要介绍了1.springboot 集成elasticsearch组件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.前置条件已经安装和搭建好了elasticsearch中间件
一:项目中引入elasticsearch相关依赖
我安装的elasticsearch版本是7.10.2 对应依赖的版本保持一致
此处省略springboot 搭建及必要的依赖项

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.10.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.10.2</version>
        </dependency>

二:项目配置文件里配置上elasticsearch相关的信息
application.yml

es:
  data:
    host: ip # es安装所在的服务器ip
    port: 9200 #es对外暴露的端口
    clusterName: hc-es-cluster #es的集群

三:工程里编写elasticsearch相关的配置代码


import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class ESConfig {

    @Value("${es.data.host}")
    private String host;
    @Value("${es.data.clusterName}")
    private String clusterName;
    @Value("${es.data.port}")
    private Integer port;


    @Bean
    public RestHighLevelClient restHighLevelClient() {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost(host, port, "http"))
        );
        return client;
    }
}

四:项目中的引入和测试文章来源地址https://www.toymoban.com/news/detail-697039.html

import cn.hutool.json.JSONUtil;
import com.jinyi.up.user.JinyiUserProviderApplication;
import com.jinyi.up.user.esPojo.AggDimensionEnum;
import com.jinyi.up.user.esPojo.EsUser;
import com.jinyi.up.user.esPojo.EsUserSearchQueryDto;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author huangchong
 * @date 2023/9/2 14:36
 * @desc
 */
@SpringBootTest(classes = {JinyiUserProviderApplication.class})
public class TestES {

    @Resource
    private RestHighLevelClient esClient;

    /**
     * 创建索引
     *
     * @throws IOException
     */
    @Test
    public void createIndex() throws IOException {
        //1.创建索引请求
        CreateIndexRequest request = new CreateIndexRequest("hc_index");
        //2.执行创建请求
        CreateIndexResponse indexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println(indexResponse);
    }

    /**
     * 获取索引 判断是否存在
     *
     * @throws IOException
     */
    @Test
    public void getIndex() throws IOException {
        //1.创建索引请求
        GetIndexRequest request = new GetIndexRequest("hc_index");
        //2.执行存在请求
        Boolean exists = esClient.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    /**
     * 删除索引
     *
     * @throws IOException
     */
    @Test
    public void deleteIndex() throws IOException {
        //1.创建索引请求
        DeleteIndexRequest request = new DeleteIndexRequest("hc_index");
        //2.执行删除请求
        AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
        System.out.println(response.isAcknowledged());
    }

    /**
     * 添加文档
     *
     * @throws IOException
     */
    @Test
    public void addEsDocument() throws IOException {
        EsUser esUser = new EsUser(1L, "hc", 32, 12, new Date());
        //1.指定索引库
        IndexRequest request = new IndexRequest("hc_index");
        // PUT /hc_index/_doc/1
        request.id("1")
                .timeout(TimeValue.timeValueSeconds(2));
        //2.将数据放入请求
        request.source(JSONUtil.toJsonStr(esUser), XContentType.JSON);
        //发送请求

        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.toString());
    }

    /**
     * 判断文档是否存在文档
     *
     * @throws IOException
     */
    @Test
    public void existsEsDocument() throws IOException {

        //1.指定索引库 和 查询id
        GetRequest request = new GetRequest("hc_index", "1");
        //不获取返回的_source 的上下文
        request.fetchSourceContext(new FetchSourceContext(false));
        request.storedFields("_none_");
        boolean exists = esClient.exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

    /**
     * 获取文档
     *
     * @throws IOException
     */
    @Test
    public void getEsDocument() throws IOException {
        //1.指定索引库 和 查询id
        GetRequest request = new GetRequest("hc_index", "1");
        //不获取返回的_source 的上下文
        //  request.fetchSourceContext(new FetchSourceContext(false));
        //  request.storedFields("_none_");
        GetResponse getResponse = esClient.get(request, RequestOptions.DEFAULT);
        System.out.println(getResponse.toString());
        String sourceAsString = getResponse.getSourceAsString();
        System.out.println(sourceAsString); //返回全部内容 ,和命令一样
    }

    /**
     * 更新文档
     *
     * @throws IOException
     */
    @Test
    public void updateEsDocument() throws IOException {
        //1.指定索引库 和 查询id
        GetRequest request = new GetRequest("hc_index", "1");
        GetResponse getResponse = esClient.get(request, RequestOptions.DEFAULT);
        System.out.println(getResponse.toString());
        String sourceAsString = getResponse.getSourceAsString();
        EsUser esUser = JSONUtil.toBean(sourceAsString, EsUser.class);
        //1.指定索引库 和 查询id
        UpdateRequest updateRequest = new UpdateRequest("hc_index", String.valueOf(esUser.getId()));
        //不获取返回的_source 的上下文
        updateRequest.timeout(TimeValue.timeValueSeconds(2));
        esUser.setUserName("huangchong 2");
        updateRequest.doc(JSONUtil.toJsonStr(esUser), XContentType.JSON);
        UpdateResponse updateResponse = esClient.update(updateRequest, RequestOptions.DEFAULT);
        System.out.println(updateResponse.toString());
    }

    /**
     * 删除文档
     *
     * @throws IOException
     */
    @Test
    public void deleteEsDocument() throws IOException {
        //1.指定索引库 和 查询id
        DeleteRequest deleteRequest = new DeleteRequest("hc_index", "1");
        deleteRequest.timeout("1s");
        DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete.toString());
    }


    /**
     * 批量操作
     *
     * @throws IOException
     */
    @Test
    public void bulkAddEsDocument() throws IOException {
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.timeout("10s");
        List<EsUser> esUsers = new ArrayList<EsUser>();
        esUsers.add(new EsUser(11L, "hc1", 31, 1, new Date()));
        esUsers.add(new EsUser(17L, "hc2", 31, 1, new Date()));
        esUsers.add(new EsUser(18L, "hc3", 28, 2, new Date()));
        esUsers.add(new EsUser(19L, "hc4", 25, 2, new Date()));
        esUsers.add(new EsUser(20L, "hc5", 19, 3, new Date()));
        //批量插入
        for (int i = 0; i < esUsers.size(); i++) {
            EsUser esUser = esUsers.get(i);
            bulkRequest.add(new IndexRequest("hc_index").id(String.valueOf(esUser.getId()))
                    .source(JSONUtil.toJsonStr(esUser), XContentType.JSON).opType(DocWriteRequest.OpType.CREATE));
        }
        esClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }

    /**
     * private AggregatorFactories.Builder aggregations;
     * 1.SearchRequest 请求
     * 2.SearchSourceBuilder 条件构建
     * searchSourceBuilder.highlighter() 高亮
     *
     * @throws IOException
     */
    @Test
    public void queryEsDocument() throws IOException {
        // 1.指定索引库
        SearchRequest searchRequest = new SearchRequest("hc_index");
        //2.构建查询请求条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //QueryBuilders工具类 termQuery 精确查询
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("userName", "hc4");

        searchSourceBuilder.query(termQueryBuilder);
        searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60L));

        searchRequest.source(searchSourceBuilder);

        SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits searchHits = response.getHits();
        SearchHit[] hits = searchHits.getHits();

        for (int i = 0; i < hits.length; i++) {
            SearchHit hit = hits[i];
            String sourceAsString = hit.getSourceAsString();
            System.out.println(JSONUtil.toBean(sourceAsString, EsUser.class).toString());
        }
    }

    /**
     * private AggregatorFactories.Builder aggregations;
     * 1.SearchRequest 请求
     * 2.SearchSourceBuilder 条件构建
     * searchSourceBuilder.highlighter() 高亮
     *
     * @throws IOException
     */
    @Test
    public void aggsEsDocument() throws IOException {
        //对hc_index库的查询请求
        SearchRequest searchRequest = new SearchRequest("hc_index");
        //paramDto 转BoolQueryBuilder :对查询条件统一封装
        EsUserSearchQueryDto queryDto = new EsUserSearchQueryDto();
        queryDto.setLikeWords("hc");
        queryDto.setUserStatus("1,2,3");
        queryDto.setAggDimensions("age");

        BoolQueryBuilder boolQueryBuilder = buildQuery(queryDto);
        //查询条件构建
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        //聚合分析无需得到分页结果,只要聚合结果就行
        buildQueryFromSize(searchSourceBuilder, 1, 0);
        //构建聚合
        aggregationsBuild(searchSourceBuilder, queryDto);
        searchRequest.source(searchSourceBuilder);
        //获取结果
        SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);
        Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();
        ParsedValueCount idNumAggs = (ParsedValueCount) aggregationMap.get("idNum");
        ParsedSum ageSumAggs = (ParsedSum) aggregationMap.get("ageSum");
        ParsedAvg ageAveAggs = (ParsedAvg) aggregationMap.get("ageAve");
        ParsedCardinality userNumAggs = (ParsedCardinality) aggregationMap.get("userStatusCount");

    }

    /**
     * 构建聚合方式
     */
    private void aggregationsBuild(SearchSourceBuilder sourceBuilder, EsUserSearchQueryDto queryDto) {

        ValueCountAggregationBuilder idNumAggs = AggregationBuilders.count("idNum").field("id");
        SumAggregationBuilder ageSumAggs = AggregationBuilders.sum("ageSum").field("age");
        //平均值
        AvgAggregationBuilder ageAvgAggs = AggregationBuilders.avg("ageAve").field("age");
        //统计去重后的数量
        CardinalityAggregationBuilder userStatusAggs = AggregationBuilders.cardinality("userStatusCount").field("userStatus");

        sourceBuilder.aggregation(idNumAggs);
        sourceBuilder.aggregation(ageSumAggs);
        sourceBuilder.aggregation(userStatusAggs);
        sourceBuilder.aggregation(ageAvgAggs);

        if (StringUtils.isNotEmpty(queryDto.getAggDimensions())) {
            List<String> list = Arrays.asList(queryDto.getAggDimensions().split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());
            for (String dimension : list) {
                AggDimensionEnum dimensionEnum = AggDimensionEnum.getByDimension(dimension);
                if (dimension == null) {
                    continue;
                }
                TermsAggregationBuilder aggs = AggregationBuilders.terms(dimensionEnum.getDimension()).field(dimensionEnum.getField())
                        .size(10000);
                sourceBuilder.aggregation(aggs);
                aggs.subAggregation(idNumAggs);
                aggs.subAggregation(ageSumAggs);
                aggs.subAggregation(ageAvgAggs);
                aggs.subAggregation(userStatusAggs);
            }
        }
    }


    protected void buildQueryFromSize(SearchSourceBuilder sourceBuilder, Integer start, Integer size) {
        sourceBuilder.from(start);
        sourceBuilder.size(size);
    }


    /**
     * 构建查询条件
     *
     * @param queryDto
     * @return
     */
    private BoolQueryBuilder buildQuery(EsUserSearchQueryDto queryDto) {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        //搜索  分词
        if (StringUtils.isNotEmpty(queryDto.getUserName())) {
            query.must(QueryBuilders.matchQuery("userName", queryDto.getUserName()));
        }
        //匹配  包含  模糊查询
        if (StringUtils.isNotEmpty(queryDto.getLikeWords())) {
            query.must(QueryBuilders.wildcardQuery("userName", "*" + queryDto.getLikeWords() + "*"));
        }
        //订单状态  多状态 or查询
        if (StringUtils.isNotEmpty(queryDto.getUserStatus())) {
            String[] orderStatuss = StringUtils.split(queryDto.getUserStatus(), ",");
            BoolQueryBuilder orQuery = QueryBuilders.boolQuery()
                    // 多个状态查询
                    .should(QueryBuilders.termsQuery("userStatus", orderStatuss));
            query.must(orQuery);
        }
        //订单创建时间
        if (queryDto.getBeginCreateTime() != null) {
            query.must(QueryBuilders.rangeQuery("createTime").gte(queryDto.getBeginCreateTime().getTime()));
        }
        if (queryDto.getEndCreateTime() != null) {
            query.must(QueryBuilders.rangeQuery("createTime").lte(queryDto.getEndCreateTime().getTime()));
        }

        return query;
    }
}

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author huangchong
 * @date 2023/9/2 14:51
 * @desc
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class EsUser {

    private Long id;

    private String userName;

    private Integer age;

    private Integer userStatus;

    private Date createTime;

}
import lombok.Data;

import java.util.Date;

/**
 * @author huangchong
 * @date 2023/9/3 16:12
 * @desc
 */
@Data
public class EsUserSearchQueryDto {

    private Long id;

    private String userName;

    private String likeWords;

    private Integer age;
    private String userStatus;

    private Date beginCreateTime;

    private Date endCreateTime;

    private String aggDimensions;
}
package com.jinyi.up.user.esPojo;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 聚合分析维度
 * @author huangchong
 * @date 2023/9/3 17:00
 * @desc
 */
public enum AggDimensionEnum {
    userStatus("userStatus", "userStatus", "用户状态"),
    age("age", "age", "年纪类型"),
    name("userName", "userName", "名字")
    ;

    private final String dimension;
    private final String field;
    private final String desc;

    AggDimensionEnum(String dimension, String field, String desc) {
        this.dimension = dimension;
        this.field = field;
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    public String getDimension() {
        return dimension;
    }

    public String getField() {
        return field;
    }


    private static final Map<String, String> aggDimensionEnumMap = Arrays.stream(values()).collect(Collectors.toMap(AggDimensionEnum::getDimension, AggDimensionEnum::getDesc));

    public static Map<String, String> getAggDimensionEnumMap() {
        return aggDimensionEnumMap;
    }

    public static AggDimensionEnum getByDimension(String dimension) {
        for (AggDimensionEnum dimensionEnum : values()) {
            if (dimensionEnum.dimension.equals(dimension)) {
                return dimensionEnum;
            }
        }
        return null;

    }
}

到了这里,关于1.springboot 集成elasticsearch组件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot集成Elasticsearch实战

    最近项目中要使用Elasticsearch所以就去简单的学习了一下怎么使用,具体的一些在高级的功能暂时展示不了,能力目前有点限,不过一些基本的需求还是可以满足的。所以就写了一篇整理一下也希望能够指出不足之处 docker部署 正常部署 首先根据spring提供的findAll方法获取所有

    2024年02月09日
    浏览(43)
  • Spring Boot 集成 Elasticsearch 实战

    @Configuration public class ElasticsearchConfiguration { @Value(“${elasticsearch.host}”) private String host; @Value(“${elasticsearch.port}”) private int port; @Value(“${elasticsearch.connTimeout}”) private int connTimeout; @Value(“${elasticsearch.socketTimeout}”) private int socketTimeout; @Value(“${elasticsearch.connectionRequestTimeout}”

    2024年04月10日
    浏览(46)
  • 【8】Spring Boot 3 集成组件:安全组件 spring security【官网概念篇】

    个人主页: 【⭐️个人主页】 需要您的【💖 点赞+关注】支持 💯 📖 本文核心知识点: spring security B 官网Doc : Spring Security Spring Security 是一个框架,它提供 身份验证 、 授权 和针对常见 攻击的保护 。它具有保护命令式和响应式应用程序的一流支持,是保护基于spring的应用

    2024年02月03日
    浏览(56)
  • 1.springboot 集成elasticsearch组件

    1.前置条件已经安装和搭建好了elasticsearch中间件 一:项目中引入elasticsearch相关依赖 我安装的elasticsearch版本是7.10.2 对应依赖的版本保持一致 此处省略springboot 搭建及必要的依赖项 二:项目配置文件里配置上elasticsearch相关的信息 application.yml 三:工程里编写elasticsearch相关的配

    2024年02月09日
    浏览(34)
  • 使用Spring Boot集成中间件:Elasticsearch基础->提高篇

    Elasticsearch是一个开源的分布式搜索和分析引擎,广泛用于构建实时的搜索和分析应用。在本篇博客中,我们将深入讲解如何使用Spring Boot集成Elasticsearch,实现数据的索引、搜索和分析。 在开始之前,确保已经完成以下准备工作: 安装并启动Elasticsearch集群 创建Elasticsearch索引

    2024年01月19日
    浏览(45)
  • Spring Boot应用集成Actuator组件以后怎么自定义端点暴露信息

    在平时业务开发中,我们往往会在spring Boot项目中集成Actuator组件进行系统监控,虽然Actuator组件暴露的端点信息已经足够丰富了,但是特殊场景下,我们也需要自己暴露端点信息,此时应该怎么操作呢? 1. 创建一个spring Boot项目,导入相关依赖 3. 暴露端点 注意: 自定义的端

    2024年02月21日
    浏览(38)
  • 如何使用Spring Cloud搭建高可用的Elasticsearch集群?详解Elasticsearch的安装与配置及Spring Boot集成的实现

    Spring Cloud 是一个基于 Spring Boot 的微服务框架,它提供了一系列组件和工具,方便开发人员快速搭建和管理分布式系统。Elasticsearch 是一个开源的全文搜索引擎,也是一个分布式、高可用的 NoSQL 数据库。本篇博客将详细讲解如何使用 Spring Cloud 搭建 Elasticsearch,并介绍如何在

    2023年04月09日
    浏览(50)
  • Spring boot 2.3.12集成ElasticSearch7.6.2并进行CRUD

    本篇博客主要讲解Spring boot 2.3.12集成ElasticSearch7.6.2并进行CRUD操作。其它版本的spring boot集成ElasticSearch类似,只需要具体各自的版本是否匹配。通过本篇博客能够成功集成ElasticSearch并进行CRUD操作,适合刚接触ElasticSearch需要进行简单CRUD操作的读者。 在集成ElasticSearch之前需要明

    2023年04月08日
    浏览(51)
  • spring boot学习第六篇:SpringBoot 集成WebSocket详解

    1、WebSocket简介 WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。 2、为什么需要WebSocket HTTP 是基于请求响应式的,即通信只能由客户端发起,服务端做出响应,无状态,无连接。 无状态:每次连

    2024年01月21日
    浏览(52)
  • 【SpringBoot3】Spring Boot 3.0 集成 Redis 缓存

    Redis缓存是一个开源的使用ANSIC语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。它主要用于作为数据库、缓存和消息中间件,以快速读写和丰富的数据结构支持而著称。 在应用程序和数据库之间,Redis缓存作为一个中间层起着关键

    2024年02月21日
    浏览(56)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包