欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > Spring Boot 集成 Elasticsearch 的详细步骤

Spring Boot 集成 Elasticsearch 的详细步骤

2025/5/4 14:45:33 来源:https://blog.csdn.net/weixin_45737215/article/details/147673558  浏览:    关键词:Spring Boot 集成 Elasticsearch 的详细步骤

以下是 Spring Boot 集成 Elasticsearch 的详细步骤:

环境安装

  • 安装 Java :Elasticsearch 基于 Java,需先安装 JDK 11 或更高版本。从官 方网站下载安装包,按教程安装配置,安装后通过命令行输入java -version验证。

  • 安装 Elasticsearch :根据官方文档选择适合系统的安装方式。如在 Windows 上可从官网下载 ZIP 包解压,Mac 用户可用 Homebrew 或官网下载,Linux 用户可从官方仓库或官网下载安装。

  • 安装 Kibana :用于可视化和管理 Elasticsearch 数据。从官网下载对应版本的 Kibana,安装过程与 Elasticsearch 类似。

创建 Spring Boot 项目

使用 Spring Initializr 快速生成项目,选择 Maven 作为构建工具,并添加“Spring Web”和“Spring Data Elasticsearch”依赖。指定项目元数据如项目名、描述、版本、包名和 Java 版本后,点击生成按钮下载项目压缩包并解压。

添加依赖

在项目的pom.xml文件中添加以下依赖:

<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>

配置 Elasticsearch 连接

src/main/resources/application.ymlapplication.properties中配置 Elasticsearch 连接信息。使用application.yml时,内容如下:

spring:data:elasticsearch:cluster-name: your-cluster-namecluster-nodes: localhost:9200

使用application.properties时,内容为:

spring.data.elasticsearch.cluster-name=your-cluster-name
spring.data.elasticsearch.cluster-nodes=localhost:9200

创建实体类

创建实体类并用@Document注解标记为 Elasticsearch 文档,例如:

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;@,Document(indexName = "user")
public class User {@Idprivate String id;private String name;private Integer age;// 省略构造方法、getter 和 setter 方法
}

创建 Repository 接口

定义一个继承自ElasticsearchRepository的接口,实现对 Elasticsearch 的 CRUD 操作。例如:

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;public interface UserRepository extends ElasticsearchRepository<User, String> {User findByName(String name);
}

使用 Repository 操作数据

在 Controller 层注入 Repository 接口,实现数据操作。例如:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
public class UserController {@Autowiredprivate UserRepository userRepository;@PostMapping("/users")public User saveUser(@RequestBody User user) {return userRepository.save(user);}@GetMapping("/users")public User getUserByName(String name) {return userRepository.findByName(name);}
}

启动 Elasticsearch 服务

确保 Elasticsearch 服务已启动。可在解压后的 Elasticsearch 目录下,通过命令./bin/elasticsearch启动,启动成功后访问http://localhost:9200可查看其状态信息。

测试集成

启动 Spring Boot 项目,使用 Postman 或 cURL 测试数据操作功能。如向POST /users发送请求添加用户数据,向GET /users?name=John发送请求查询用户数据。

进阶优化与配置

  • 自定义 Elasticsearch 客户端配置 :若需自定义连接池等配置,可通过创建配置类实现。例如:

    @Configuration
    public class ElasticsearchConfig {@Beanpublic RestHighLevelClient client() {RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));return new RestHighLevelClient(builder);}
    }
  • 性能优化 :合理设置索引的分片数和副本数,对查询进行分页和缓存优化,为重要字段设置合适的索引类型等,以提高查询性能和系统效率。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词