使用SpringBoot集成Elasticsearch实现CRUD功能
创始人
2025-01-10 05:36:00
0

添加Elasticsearch依赖

首先,在pom.xml文件中添加Spring Boot和Elasticsearch的依赖:

      org.springframework.boot     spring-boot-starter-data-elasticsearch         org.elasticsearch.client     elasticsearch-rest-high-level-client     7.16.0   

配置Elasticsearch连接信息

application.propertiesapplication.yml文件中配置Elasticsearch连接信息:

使用application.properties配置:

# Elasticsearch properties spring.elasticsearch.rest.uris=http://localhost:9200

使用application.yml配置:

spring:   elasticsearch:     rest:       uris: http://localhost:9200 

定义实体类

定义一个简单的实体类,作为索引中的文档:

import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document;  @Document(indexName = "books", createIndex = true) public class Book {      @Id     private String id;      private String title;      private String author;      // 省略构造函数、getter和setter } 

创建Elasticsearch操作接口

创建一个接口来定义对Elasticsearch的操作:

import org.springframework.data.elasticsearch.annotations.Query; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List;  public interface BookRepository extends ElasticsearchRepository {      List findByTitle(String title);      @Query("{\"match\": {\"author\": \"?0\"}}")     List findByAuthorQuery(String author); } 

实现CRUD操作

在服务类或控制器中使用BookRepository进行CRUD操作:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;  @RestController @RequestMapping("/books") public class BookController {      @Autowired     private BookRepository bookRepository;      @PostMapping     public Book addBook(@RequestBody Book book) {         return bookRepository.save(book);     }      @GetMapping("/{id}")     public Book getBook(@PathVariable String id) {         return bookRepository.findById(id).orElse(null);     }      @PutMapping("/{id}")     public Book updateBook(@PathVariable String id, @RequestBody Book book) {         book.setId(id); // 设置要更新的文档的ID         return bookRepository.save(book);     }      @DeleteMapping("/{id}")     public void deleteBook(@PathVariable String id) {         bookRepository.deleteById(id);     }      @GetMapping("/title/{title}")     public List findByTitle(@PathVariable String title) {         return bookRepository.findByTitle(title);     }      @GetMapping("/author/{author}")     public List findByAuthor(@PathVariable String author) {         return bookRepository.findByAuthorQuery(author);     } } 

示例说明

  • Book:使用@Document注解指定了索引名称和是否自动创建索引。
  • BookRepository接口:继承自ElasticsearchRepository,定义了一些基本的查询方法和自定义的查询方法。
  • BookController:使用BookRepository进行CRUD操作的示例控制器,包括添加、获取、更新和删除文档,以及根据标题和作者进行查询。

注意事项

  • 确保Elasticsearch服务器已经启动,并且配置信息(如端口、主机)正确匹配。
  • 在实际项目中,你可能需要根据具体的业务需求,定义更多的查询方法或者对查询结果进行处理。
  • 需要根据你的Elasticsearch版本来选择合适的elasticsearch-rest-high-level-client版本,并进行相应的依赖管理。

通过这些步骤,你可以在Spring Boot应用程序中轻松地集成Elasticsearch,并实现对其的CRUD操作。

相关内容

热门资讯

八分钟辅助!拱趴大菠萝万能挂图... 八分钟辅助!拱趴大菠萝万能挂图解,如何下载wpk透视版,妙招教程(有挂秘笈)1、打开软件启动之后找到...
第9分钟辅助!wepoker辅... 第9分钟辅助!wepoker辅助插件功能,hhpoker辅助软件是真的么,项目教程(有挂头条)该软件...
第五分钟辅助!wpk作弊最怕三... 第五分钟辅助!wpk作弊最怕三个东西,wepoker免费脚本,手册教程(有挂总结)1、下载好wpk作...
9分钟辅助!德普之星透视辅助,... 9分钟辅助!德普之星透视辅助,wepokerplus辅助,方式教程(有挂透明挂)1、用户打开应用后不...
1分钟辅助!wepoker透视... 1分钟辅助!wepoker透视挂底牌,hhpoker透视脚本视频,方案教程(今日头条)1、玩家可以在...
第三分钟辅助!如何下载德普之星... 第三分钟辅助!如何下载德普之星辅助软件,智星菠萝有挂吗,模块教程(有挂教程)如何下载德普之星辅助软件...
9分钟辅助!hhpoker作弊... 9分钟辅助!hhpoker作弊码,xpoker辅助工具,教程书教程(有挂分析)运xpoker辅助工具...
第4分钟辅助!pokemmo手... 第4分钟辅助!pokemmo手机脚本,werplan辅助软件,总结教程(真是有挂)1、首先打开pok...
9分钟辅助!德州真人透视脚本,... 9分钟辅助!德州真人透视脚本,pokemmo脚本辅助器下载,妙计教程(存在有挂)1、超多福利:超高返...
第五分钟辅助!德普之星怎么设置... 您好,德普之星怎么设置埋牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很...