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

相关推荐
小冷coding11 天前
【面试】面试官:请举例回答Elasticsearch的数据模型有哪些核心概念?
java·分布式·elasticsearch·面试·es
墨痕诉清风20 天前
AWS OpenSearch Dev Tools使用
aws·es·opensearch
罗技12321 天前
用Filebeat OSS 7.10.2将收集日志到Easysearch
运维·es
feilieren1 个月前
Docker 安装 Elasticsearch 9
运维·elasticsearch·docker·es
墨痕诉清风1 个月前
OpenSearch添加仪表盘(elastic、es)
aws·es·waf·opensearch·elastic
wangfy_1 个月前
Elasticsearch、Faiss、Milvus在向量索引实现上的核心差
es
Gold Steps.2 个月前
Docker容器部署elasticsearch8.*与Kibana8.*版本使用filebeat采集日志
运维·docker·云原生·es
Gold Steps.2 个月前
从0到1认识ElasticStack
elk·云原生·es
赵成默2 个月前
es 的字段类型(text和keyword)
elasticsearch·es
G皮T2 个月前
【Elasticsearch】Elasticsearch 核心技术(一):索引
大数据·elasticsearch·kibana·es·索引·索引别名·索引模板