SpringBoot 3整合Elasticsearch 8

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

版本说明

官网说明
springboot 使用 es8.11,spring boot,elasticsearch,ssh,spring data,https
本文使用最新的版本

springboot: 3.2.3
spring-data elasticsearch: 5.2.3
elasticsearch: 8.11.4

elasticsearch下载链接:https://www.elastic.co/cn/downloads/past-releases#elasticsearch

最新版可能不兼容,以spring官网为准

spring boot POM依赖

<?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, "北京朝阳"));
        esUserRepository.save(new UserModel(3, "王五", 20, "北京朝阳"));
        esUserRepository.save(new UserModel(4, "赵六", 21, "北京朝阳"));
        esUserRepository.save(new UserModel(5, "马六", 22, "北京朝阳"));
        esUserRepository.save(new UserModel(6, "孙七", 23, "北京朝阳"));
        esUserRepository.save(new UserModel(7, "吴八", 24, "北京朝阳"));
        esUserRepository.save(new UserModel(8, "郑九", 25, "北京朝阳"));

        // 查询所有
        esUserRepository.findAll().forEach(System.out::println);
    }

    /**
     * 更新文档
     */
    @Test
    public void testUpdate() {
        // 按id更新
        IndexCoordinates indexCoordinates = elasticsearchOperations.indexOps(UserModel.class).getIndexCoordinates();
        documentOperations.update(new UserModel(1, "张三", 60, "北京朝阳"), indexCoordinates);
    }

    /**
     * 删除文档
     */
    @Test
    public void testDelete() {
        documentOperations.delete(String.valueOf(8), UserModel.class);
    }

    /**
     * 查询文档
     */
    @Test
    public void testSearch() {
        CriteriaQuery query = new CriteriaQuery(new Criteria("id").is(2));
        SearchHits<UserModel> searchHits = searchOperations.search(query, UserModel.class);
        for (SearchHit searchHit : searchHits.getSearchHits()){
            UserModel user = (UserModel) searchHit.getContent();
            System.out.println(user);
        }
    }

}

完整项目文件目录结构

springboot 使用 es8.11,spring boot,elasticsearch,ssh,spring data,https

windows下elasticsearch安装配置

直接解压修改配置文件解压目录/config/elasticsearch.yml


# 集群名称
cluster.name: el-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
# 节点名称
node.name: el-node-1

#  数据和日志存储路径,默认安装位置

path.data: D:/module/elasticsearch-8.11.4/data

path.logs: D:/module/elasticsearch-8.11.4/logs

# 访问限制,0.0.0.0代表所有IP都可以访问,localhost也可以
network.host: 0.0.0.0
# 访问端口 默认9200
http.port: 9200


# 安全配置,以下的配置第一次启动时自动生成,也可以不配置
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically      
# generated to configure Elasticsearch security features on 21-03-2024 01:32:15
#
# --------------------------------------------------------------------------------

# Enable security features 不使用https时设为false
xpack.security.enabled: true

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents 不使用https时设为false
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
cluster.initial_master_nodes: ["el-node-1"]

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

第一次启动会在控制台打印密码,用户名默认elastic
修改密码的话不要关闭控制台,另外开启一个控制台,进入elastic search安装目录下的bin目录,使用以下命令修改
-i 是交互式的意思,没有的话会随机生成密码,无法自定义。
输入命令回车然后输入两次密码就行了

elasticsearch-reset-password --username elastic -i

使用keytool工具将ca证书导入到jdk。
keytool是jdk自带的工具,使用以下命令

keytool -importcert -cacerts -alias "es_http_ca" -file "elasticsearch安装路径\config\certs\http_ca.crt"

es_http_ca 是证书别名文章来源地址https://www.toymoban.com/news/detail-847969.html

到了这里,关于SpringBoot 3整合Elasticsearch 8的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 【Elasticsearch】从零开始搭建ES8集群并且集成到Springboot,更好的服务电商类等需要全文索引的项目(一)

    最近公司的电商项目越来越庞大,功能需求点也越来越多,各种C端对查询和检索的要求也越来越高,是时候在项目中引入全文检索了。 ElasticSearch 是一个基于 Lucene 的搜索服务器,它提供了一个分布式多用户能力的全文搜索引擎,并且是基于Java 开发的,我记得很久之前ES还不

    2024年02月15日
    浏览(39)
  • ElasticSearch的使用,安装ik分词器,自定义词库,SpringBoot整合ES(增、删、改、查)

    保存一个数据,保存在哪个索引的哪个类型下,指定用哪个唯一标识(相当于,保存一个数据,保存在那个数据库中的哪个表中,指定主键ID) 例:PUT customer/external/1;在customer索引下的external类型下保存1号数据name为John Doe的数据 POST和PUT都可以新增数据 注意: POST 新增。如果

    2023年04月25日
    浏览(40)
  • Elasticsearch基础,SpringBoot整合Elasticsearch

    Elasticsearch,简称为es,es是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别(大数据时代)的数据。es也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通

    2024年01月19日
    浏览(35)
  • 【ElasticSearch系列-05】SpringBoot整合elasticSearch

    ElasticSearch系列整体栏目 内容 链接地址 【一】ElasticSearch下载和安装 https://zhenghuisheng.blog.csdn.net/article/details/129260827 【二】ElasticSearch概念和基本操作 https://blog.csdn.net/zhenghuishengq/article/details/134121631 【三】ElasticSearch的高级查询Query DSL https://blog.csdn.net/zhenghuishengq/article/details/1

    2024年02月06日
    浏览(32)
  • ElasticSearch8 - SpringBoot整合ElasticSearch

    springboot 整合 ES 有两种方案,ES 官方提供的 Elasticsearch Java API Client 和 spring 提供的 [Spring Data Elasticsearch](Spring Data Elasticsearch) 两种方案各有优劣 Spring:高度封装,用着舒服。缺点是更新不及时,有可能无法使用 ES 的新 API ES 官方:更新及时,灵活,缺点是太灵活了,基本是一

    2024年03月25日
    浏览(85)
  • 三.SpringBoot整合Elasticsearch

    我们整合es直接给es发请求就可以了,但是现在有很多方式去调用es的接口,那都有那些呢? 访问es端口 访问方式 使用工具 缺点 9300 TCP transport-api.jar 不适配es版本,es 8.0之后弃用。 9200 HTTP JestClient 非官方,对应es版本更新慢。 9200 HTTP RestTemplate 模拟发送http请求,但是很多请求

    2024年02月13日
    浏览(27)
  • SpringBoot 整合 ElasticSearch

    😍开始前给大家推荐一款很火的刷题、面试求职网站💕 https://www.nowcoder.com/link/pc_csdncpt_xiaoying_java 索引Index 一组相似文档的集合 一个索引就是一个拥有几分相似特征的文档的集合。比如说,你可以有一个商品数据的索引,一个订单数据的索引,还有一个用户数据的索引。一

    2023年04月08日
    浏览(25)
  • ElasticSearch(九)【SpringBoot整合】

    上一篇文章 《ElasticSearch - 过滤查询》 9.1 基本环境配置 创建一个springboot工程 springboot-elasticsearch 在 pom.xml 导入依赖 【 注意 】使用的springboot需要根当前ES版本兼容 配置 application.yml 文件 配置客户端 创建config包,添加配置类 RestClientConfiguration.class 配置完之后,该配置类不仅

    2024年02月14日
    浏览(26)
  • Springboot整合Elasticsearch(Es)

    首先 在测试类中引入RestHighLevelClient对象 其次 准备一个User对象 3.1.1 创建索引  运行结果:创建成功返回true 3.1.2 删除索引 运行结果:删除成功返回true  3.1.3 判断索引是否存在 运行结果:存在返回true,不存在返回false. 3.2.1 添加文档 运行结果:添加成功返回 CREATED 3.2.2 查询文档--

    2023年04月22日
    浏览(33)
  • SpringBoot 3整合Elasticsearch 8

    官网说明 本文使用最新的版本 springboot: 3.2.3 spring-data elasticsearch: 5.2.3 elasticsearch: 8.11.4 elasticsearch下载链接:https://www.elastic.co/cn/downloads/past-releases#elasticsearch 最新版可能不兼容,以spring官网为准 使用 https 必须配置 username 和 password spring data的repository方便操作,类似jpa的操作

    2024年04月11日
    浏览(19)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包