【SpringBoot中怎么使用ElasticSearch】

springboot中怎么使用ElasticSearch

在Spring Boot中使用ElasticSearch,可以通过集成Spring Data ElasticSearch来实现。以下是一个详细的步骤指南,帮助你设置和使用ElasticSearch与Spring Boot。

1. 添加依赖

首先,在你的Spring Boot项目的pom.xml文件中添加Spring Data ElasticSearch的依赖。

xml 复制代码
<dependencies>
    <!-- Spring Boot Starter Data ElasticSearch -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    
    <!-- 如果使用Java REST High Level Client,可以添加这个依赖 -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.10.2</version> <!-- 确保版本与你的ElasticSearch服务器匹配 -->
    </dependency>
    
    <!-- 其他依赖 -->
</dependencies>

2. 配置ElasticSearch

application.propertiesapplication.yml中配置ElasticSearch的连接信息。

properties 复制代码
# application.properties
spring.elasticsearch.rest.uris=http://localhost:9200

或者

yaml 复制代码
# application.yml
spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200

3. 创建实体类

创建一个实体类,并使用Spring Data ElasticSearch的注解来映射ElasticSearch的索引和文档。

java 复制代码
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
 
@Document(indexName = "my-index")
public class MyEntity {
 
    @Id
    private String id;
    private String name;
    private String description;
 
    // Getters and Setters
}

4. 创建Repository接口

创建一个继承自ElasticsearchRepository的接口,用于数据访问。

java 复制代码
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
 
public interface MyEntityRepository extends ElasticsearchRepository<MyEntity, String> {
    // 自定义查询方法可以在这里定义,如果需要的话
}

5. 使用Service和Controller

在Service中注入Repository,并编写业务逻辑。

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service
public class MyEntityService {
 
    @Autowired
    private MyEntityRepository repository;
 
    public List<MyEntity> findAll() {
        return repository.findAll();
    }
 
    public MyEntity save(MyEntity entity) {
        return repository.save(entity);
    }
 
    // 其他业务逻辑
}

在Controller中调用Service,并处理HTTP请求。

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import java.util.List;
 
@RestController
@RequestMapping("/my-entities")
public class MyEntityController {
 
    @Autowired
    private MyEntityService service;
 
    @GetMapping
    public List<MyEntity> getAllEntities() {
        return service.findAll();
    }
 
    @PostMapping
    public MyEntity createEntity(@RequestBody MyEntity entity) {
        return service.save(entity);
    }
 
    // 其他HTTP处理方法
}

6. 启动应用程序

确保ElasticSearch服务器正在运行,然后启动你的Spring Boot应用程序。你现在应该能够通过HTTP请求与ElasticSearch进行交互。

7. 测试

你可以使用Postman或curl来测试你的API。例如,创建一个新的实体:

bash 复制代码
curl -X POST "http://localhost:8080/my-entities" -H "Content-Type: application/json" -d '{
    "id": "1",
    "name": "Test Entity",
    "description": "This is a test entity."
}'

获取所有实体:

bash 复制代码
bash复制代码

curl -X GET "http://localhost:8080/my-entities"

这样,你就完成了在Spring Boot中集成和使用ElasticSearch的基本步骤。如果需要更高级的功能,比如自定义查询、分页、排序等,可以参考Spring Data ElasticSearch的官方文档。或者这个文档https://blog.csdn.net/ZGL_cyy/article/details/112796302

相关推荐
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
计算机-秋大田4 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
Elastic 中国社区官方博客5 小时前
使用真实 Elasticsearch 进行高级集成测试
大数据·数据库·elasticsearch·搜索引擎·全文检索·jenkins·集成测试
customer087 小时前
【开源免费】基于SpringBoot+Vue.JS景区民宿预约系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
精通HelloWorld!12 小时前
使用HttpClient和HttpRequest发送HTTP请求
java·spring boot·网络协议·spring·http
画船听雨眠aa12 小时前
gitlab云服务器配置
服务器·git·elasticsearch·gitlab
拾忆,想起13 小时前
如何选择Spring AOP的动态代理?JDK与CGLIB的适用场景
spring boot·后端·spring·spring cloud·微服务
customer0815 小时前
【开源免费】基于SpringBoot+Vue.JS美食推荐商城(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
一 乐16 小时前
基于微信小程序的酒店管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·微信小程序·酒店管理系统
risc12345616 小时前
【Elasticsearch 】悬挂索引(Dangling Indices)
大数据·elasticsearch·搜索引擎