设为首页 加入收藏

TOP

SpringBoot3集成ElasticSearch(一)
2023-08-26 21:11:04 】 浏览:52
Tags:SpringBoot3 集成 ElasticSearch

标签:ElasticSearch8.Kibana8;

一、简介

Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,适用于各种数据类型,数字、文本、地理位置、结构化数据、非结构化数据;

在实际的工作中,历经过Elasticsearch从6.07.0的版本升级,而这次SpringBoot3和ES8.0的集成,虽然脚本的语法变化很小,但是Java客户端的API语法变化很大;

二、环境搭建

1、下载安装包

需要注意的是,这些安装包的版本要选择对应的,不然容易出问题;

软件包:elasticsearch-8.8.2-darwin-x86_64.tar.gz
分词器工具:elasticsearch-analysis-ik-8.8.2.zip
可视化工具:kibana-8.8.2-darwin-x86_64.tar.gz

2、服务启动

不论是ES还是Kibana,在首次启动后,会初始化很多配置文件,可以根据自己的需要做相关的配置调整,比如常见的端口调整,资源占用,安全校验等;

1、启动ES
elasticsearch-8.8.2/bin/elasticsearch

本地访问:localhost:9200

2、启动Kibana
kibana-8.8.2/bin/kibana

本地访问:http://localhost:5601

# 3、查看安装的插件
http://localhost:9200/_cat/plugins  ->  analysis-ik 8.8.2

三、工程搭建

1、工程结构

2、依赖管理

starter-elasticsearch组件中,实际上依赖的是elasticsearch-java组件的8.7.1版本;

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>${spring-boot.version}</version>
</dependency>

3、配置文件

在上面环境搭建的过程中,已经禁用了用户和密码的登录验证,配置ES服务地址即可;

spring:
  # ElasticSearch配置
  elasticsearch:
    uris: localhost:9200

四、基础用法

1、实体类

通过DocumentField注解描述ES索引结构的实体类,注意这里JsonIgnoreProperties注解,解决索引中字段和实体类非一一对应的而引起的JSON解析问题;

@JsonIgnoreProperties(ignoreUnknown = true)
@Document(indexName = "contents_index", createIndex = false)
public class ContentsIndex implements Serializable {

    private static final long serialVersionUID=1L;

    @Field(type= FieldType.Integer)
    private Integer id;

    @Field(type= FieldType.Keyword)
    private String title;

    @Field(type= FieldType.Keyword)
    private String intro;

    @Field(type= FieldType.Text)
    private String content;

    @Field(type= FieldType.Integer)
    private Integer createId;

    @Field(type= FieldType.Keyword)
    private String createName;

    @Field(type= FieldType.Date,format = DateFormat.date_hour_minute_second)
    private Date createTime;
}

2、初始化索引

基于ElasticsearchTemplate类和上述实体类,实现索引结构的初始化,并且将tb_contents表中的数据同步到索引中,最后通过ID查询一条测试数据;

@Service
public class ContentsIndexService {
    private static final Logger log = LoggerFactory.getLogger(ContentsIndexService.class);

    @Resource
    private ContentsService contentsService ;
    @Resource
    private ElasticsearchTemplate template ;

    /**
     * 初始化索引结构和数据
     */
    public void initIndex (){
        // 处理索引结构
        IndexOperations indexOps = template.indexOps(ContentsIndex.class);
        if (indexOps.exists()){
            boolean delFlag = indexOps.delete();
            log.info("contents_index exists,delete:{}",delFlag);
            indexOps.createMapping(ContentsIndex.class);
        } else {
            log.info("contents_index not exists");
            indexOps.createMapping(ContentsIndex.class);
        }
        // 同步数据库表记录
        List<Contents> contentsList = contentsService.queryAll();
        if (contentsList.size() > 0){
            List<ContentsIndex> contentsIndexList = new ArrayList<>() ;
            contentsList.forEach(contents -> {
                ContentsIndex contentsIndex = new ContentsIndex() ;
                BeanUtils.copyProperties(contents,contentsIndex);
                contentsIndexList.add(contentsIndex);
            });
            template.save(contentsIndexList);
        }
        // ID查询
        ContentsInd
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Sping aop 以及Spring aop 的应用.. 下一篇如何分析 JVM 内存瓶颈浅谈

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目