SpringBoot 3整合Elasticsearch 8_springboot 使用 es8

本文介绍了如何在SpringBoot 3应用中整合Elasticsearch 8,包括application.yml的配置,创建模型映射,利用Spring Data的Repository进行数据操作,并进行了简单的测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo-es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-es</name>
    <description>demo-es</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml配置

使用https必须配置username 和 password

spring:
  elasticsearch:
    uris: https://localhost:9200
    username: elastic
    password: 123456

新建模型映射

package com.example.demoes.es.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "user") // user 是elasticsearch的索引名称(新版本的elasticsearch没有了type的概念)
public class UserModel {  // 每一个UserModel对应一个elasticsearch的文档

    @Id
    @Field(name = "id", type = FieldType.Integer)
    Integer id;

    // FieldType.Keyword 不可分词
    @Field(name = "name", type = FieldType.Keyword)
    String name;

    // index = false 不建立索引
    @Field(name = "age", type = FieldType.Integer, index = false)
    Integer age;

    // FieldType.Text 可分词,ik\_smart,ik\_max\_word 是ik分词器,对中文分词友好,需要另外安装
    @Field(name = "address", type = FieldType.Text, searchAnalyzer = "ik\_smart", analyzer = "ik\_max\_word")
    String address;

}


Repository

spring data的repository方便操作,类似jpa的操作
继承ElasticsearchRepository自带一些基础的操作方法

package com.example.demoes.es.repo;

import com.example.demoes.es.model.UserModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

// UserModel 模型映射 Integer ID的类型
public interface ESUserRepository extends ElasticsearchRepository<UserModel, Integer> {

}

简单测试

package com.example.demoes;

import com.example.demoes.es.model.UserModel;
import com.example.demoes.es.repo.ESUserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.\*;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;


@SpringBootTest
class DemoEsApplicationTests {

    @Autowired
    ESUserRepository esUserRepository;

    // 以下三个是 spring-boot-starter-data-elasticsearch 自动配置的 elasticsearch 操作 Bean
    // 1. DocumentOperations 文档操作
    @Autowired
    DocumentOperations documentOperations;

    // 2. SearchOperations 查询操作
    @Autowired
    SearchOperations searchOperations;

    // 3. ElasticsearchOperations elasticsearch 通用的操作,包括DocumentOperations和SearchOperations
    @Autowired
    ElasticsearchOperations elasticsearchOperations;

    @Test
    void contextLoads() {
    }

    @Test
    public void testIndex() {
        // 获取索引操作
        IndexOperations indexOperations = elasticsearchOperations.indexOps(UserModel.class);
        // 查看索引映射关系
        System.out.println(indexOperations.getMapping());
        // 输出索引名称
        System.out.println(indexOperations.getIndexCoordinates().getIndexName());

    }

    /\*\*
 \* 添加文档
 \*/
    @Test
    public void testAdd() {
        esUserRepository.save(new UserModel(1, "张三", 18, "北京朝阳"));
        esUserRepository.save(new UserModel(2, "李四", 19, "北京朝阳"));


**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数Linux运维工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/fbc696d9445cc8fd976c516ad850a615.png)
![img](https://img-blog.csdnimg.cn/img_convert/84c8b779a15eb050ccb739234225a9dd.png)
![img](https://img-blog.csdnimg.cn/img_convert/ced3f8941436d6f7953adf83f87586b4.png)
![img](https://img-blog.csdnimg.cn/img_convert/eeefcc58ec6e9f1e0312b78a0b05153d.png)
![img](https://img-blog.csdnimg.cn/img_convert/9b57aa1c512ee3e2cb8c04198bda5b1f.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Linux运维知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip1024b (备注Linux运维获取)**
![img](https://img-blog.csdnimg.cn/img_convert/d423da8949e6a73ccc0f5d70baddc30e.jpeg)

![](https://img-blog.csdnimg.cn/img_convert/9a8cb5f8c0ec69e6499adead0da6e95b.png)



最全的Linux教程,Linux从入门到精通

======================

1.  **linux从入门到精通(第2版)**

2.  **Linux系统移植**

3.  **Linux驱动开发入门与实战**

4.  **LINUX 系统移植 第2版**

5.  **Linux开源网络全栈详解 从DPDK到OpenFlow**



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/59742364bb1338737fe2d315a9e2ec54.png)



第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。



![华为18级工程师呕心沥血撰写3000页Linux学习笔记教程](https://img-blog.csdnimg.cn/img_convert/9d4aefb6a92edea27b825e59aa1f2c54.png)



**本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。**

> 需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论




[**一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!**](https://bbs.youkuaiyun.com/forums/4304bb5a486d4c3ab8389e65ecb71ac0)

**AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算**

,让我们一起学习成长!**](https://bbs.youkuaiyun.com/forums/4304bb5a486d4c3ab8389e65ecb71ac0)

**AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值