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

相关推荐
信仰JR6 天前
OpenKylin安装Elastic Search8
elasticsearch·es
Cloud_.1 个月前
Spring Boot整合Elasticsearch
java·spring boot·后端·elasticsearch·es
码农爱java2 个月前
Elasticsearch 之 ElasticsearchRestTemplate 普通查询
大数据·elasticsearch·全文检索·jenkins·springboot·es·es 查询
Elastic开源社区2 个月前
Search after解决ES深度分页问题
大数据·elasticsearch·es·search after·深度分页
罗技1232 个月前
Easysearch 使用 AWS S3 进行快照备份与还原:完整指南及常见错误排查
linux·云计算·aws·es·easysearch
我的ID配享太庙呀2 个月前
Centos的ElasticSearch安装教程
运维·计算机网络·elasticsearch·centos·智能路由器·jenkins·es
Elastic开源社区2 个月前
Elastic如何获取当前系统时间
elasticsearch·es·系统时间
Hanson Huang3 个月前
【存储中间件API】MySQL、Redis、MongoDB、ES常见api操作及性能比较
redis·mysql·mongodb·es
卷心菜不卷Iris3 个月前
第1章大型互联网公司的基础架构——1.10 其他NoSQL数据库
数据库·nosql·es·互联网大厂·基础架构
平时不搬砖3 个月前
基于 Filebeat 的日志收集
开发语言·elasticsearch·es