Elasticsearch和sboot整合

Elasticsearch是一个开源的、高性能的分布式搜索引擎,可以实现全文搜索、分析和存储等功能。Spring Boot是一个开源的Java框架,可以用于快速开发和部署应用程序。

将Elasticsearch和Spring Boot整合可以使得我们更加方便地使用Elasticsearch进行搜索和数据分析。以下是整合的步骤:

  1. 添加Elasticsearch的依赖

在Spring Boot工程中,可以在pom.xml文件中添加以下依赖,其中elasticsearch和elasticsearch-rest-high-level-client是Elasticsearch相关的依赖:

复制代码
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>6.8.17</version>
</dependency>

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.8.17</version>
</dependency>
  1. 配置Elasticsearch的连接

在Spring Boot工程中,可以在application.properties或application.yml文件中添加以下配置,注意将host和port替换为Elasticsearch的实际地址和端口:

复制代码
spring.data.elasticsearch.cluster-nodes=host:port
  1. 创建Elasticsearch的客户端

在Spring Boot的项目中,可以使用RestHighLevelClient创建Elasticsearch客户端。以下是一个简单的示例代码:

复制代码
@Configuration
public class ElasticsearchClientConfig {

    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        String[] nodes = clusterNodes.split(",");
        HttpHost[] httpHosts = new HttpHost[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            String node = nodes[i];
            httpHosts[i] = new HttpHost(node.split(":")[0], Integer.parseInt(node.split(":")[1]), "http");
        }
        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }
}
  1. 创建Elasticsearch的存储库

在Spring Boot的项目中,可以使用ElasticsearchRepository来操作Elasticsearch中的数据。以下是一个示例代码:

复制代码
public interface BookRepository extends ElasticsearchRepository<Book, String> {

    List<Book> findByTitle(String title);

    List<Book> findByAuthor(String author);
}

其中,Book是用于存储书籍信息的实体类,可以根据需要进行定义。在该接口中,实现了根据标题和作者进行搜索的功能。

  1. 进行数据操作

在Spring Boot的项目中,可以使用ElasticsearchRepository中定义的方法来进行数据操作。以下是一个示例代码:

复制代码
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @PostMapping("/")
    public Book save(@RequestBody Book book) {
        return bookRepository.save(book);
    }

    @GetMapping("/{id}")
    public Book getById(@PathVariable("id") String id) {
        return bookRepository.findById(id).orElse(null);
    }

    @GetMapping("/")
    public List<Book> getByTitle(@RequestParam("title") String title) {
        return bookRepository.findByTitle(title);
    }
}

以上代码实现了通过RESTful API进行数据操作的功能,包括保存图书信息、根据ID查询图书信息、根据标题查询图书信息等。

相关推荐
huisheng_qaq4 天前
【项目篇-01】Vmware虚拟机和环境安装配置
redis·mysql·canal·rocketmq·es·vaware虚拟机
JAVA面经实录9174 天前
Elasticsearch 完整版完整知识体系
java·elasticsearch·搜索引擎·es
代码讲故事5 天前
在没有kibana的ES(elasticsearch)线上生产环境集群中,如何通过命令行修改或增加字段而不需要reindex?
大数据·elasticsearch·搜索引擎·命令行·es·索引·模版
yurenpai(27届找实习中)12 天前
Elasticsearch 核心总结 + 面试题实战(黑马点评项目)
redis·es
徐小青青啊13 天前
es集群不中断实时数据更新损坏节点硬盘
大数据·elasticsearch·搜索引擎·es
醇氧16 天前
【Hermes Agent】使用阿里云百炼 Token Plan(方式一:命令行一键配置)
阿里云·云计算·es·openclaw
RemainderTime1 个月前
基于Spring AI + 阿里百炼 DashScope:构建 AI Agent RAG 企业级知识助手
人工智能·后端·spring·ai·es
Johnstons1 个月前
Wireshark 和 tcpdump 到底怎么选?网络故障排查实战中的边界、判断标准与落地清单
wireshark·php·es·tcpdump·抓包分析·抓包与协议分析工具选型
Johnstons1 个月前
TCP Reset(RST)异常是什么?一文讲透连接被动中断的识别方法、适用场景、与超时断开的边界及排查清单
网络协议·tcp/ip·php·es·抓包分析
Johnstons2 个月前
Wireshark ExpertInfo是什么?一文讲透异常分级、适用场景、和传统抓包阅读的区别与排查标准
网络·测试工具·wireshark·es