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

相关推荐
Johnstons4 小时前
TCP Reset(RST)异常是什么?一文讲透连接被动中断的识别方法、适用场景、与超时断开的边界及排查清单
网络协议·tcp/ip·php·es·抓包分析
Johnstons3 天前
Wireshark ExpertInfo是什么?一文讲透异常分级、适用场景、和传统抓包阅读的区别与排查标准
网络·测试工具·wireshark·es
Johnstons7 天前
网络故障定位工具怎么搭配:Wireshark、tcpdump、监控平台各自该在什么时候上场?
数据分析·wireshark·php·es·tcpdump·网络故障定位工具搭配与选型
Johnstons8 天前
网络诊断工具怎么选:从监控告警到抓包定位的完整方法论
服务器·网络·php·es·抓包分析·网络诊断工具选型与排障方法
Johnstons8 天前
抓包工具怎么选:Wireshark、tcpdump 与流量回溯平台的边界、场景与排障判断标准
测试工具·数据分析·wireshark·es·tcpdump·抓包工具选型与流量回溯
深念Y23 天前
从字典到向量:索引技术的演进
向量·es·索引·倒排索引·向量数据库·字典·倒排文件索引
A__tao1 个月前
Elasticsearch Mapping 一键生成 Go Struct,支持嵌套解析
elasticsearch·es
A__tao1 个月前
告别手写!ES Mapping 自动生成 Go Struct(支持嵌套)
elasticsearch·golang·es
尽兴-1 个月前
微服务日志采集与分析系统实战:ELK 架构全解析与落地
elk·微服务·架构·kibana·es·logstash·filebeat
rainFFrain1 个月前
Elasticsearch索引创建与搜索
es