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查询图书信息、根据标题查询图书信息等。

相关推荐
长路 ㅤ   1 天前
ES理论:分页查询方案及优化策略
es·scroll·search_after·elasticsearch分页·from+size·pit
Wang's Blog13 天前
Elastic Stack梳理: 关联关系处理方案深度解析与工程实践
搜索引擎·es·elastic search
Wang's Blog14 天前
Elastic Stack梳理: 聚合分析核心技术深度解析与最佳实践
elasticsearch·搜索引擎·es·elastic search
qiyongwork16 天前
挣值管理中引入ES——更准确的用时间维度监控项目执行
es·evm·earned value·挣值管理
safestar201217 天前
Elasticsearch分片设计:从数据分布失衡到集群稳定性实战
java·es
safestar201217 天前
Elasticsearch ILM实战:从数据热恋到冷静归档的自动化管理
java·开发语言·jvm·elasticsearch·es
safestar201218 天前
Elasticsearch深度实战:从分布式原理到生产环境踩坑全记录
运维·搜索引擎·全文检索·es
G皮T18 天前
【Elasticsearch】索引别名 aliases
大数据·elasticsearch·搜索引擎·es·索引·索引别名·aliases
ζั͡山 ั͡有扶苏 ั͡✾24 天前
EFK 日志系统搭建完整教程
运维·jenkins·kibana·es·filebeat
ζั͡山 ั͡有扶苏 ั͡✾24 天前
ES日志收集与AI智能分析程序
es·日志收集和报告