使用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操作。

相关内容

热门资讯

玩家必看科普!微乐小程序辅助工... 玩家必看科普!微乐小程序辅助工具!在哪里下载微乐自建房辅助软件(一直真的有辅助app)运辅助工具,进...
今日科普!微乐小程序辅助工具!... 今日科普!微乐小程序辅助工具!微乐小程序游戏破解器下载(真是真的有辅助器);1、有没有辅助教程、有透...
大家学习交流!微乐小程序自建房... 大家学习交流!微乐小程序自建房辅助!微乐小程序游戏破解器(都是存在有辅助脚本)1、每一步都需要思考,...
终于知道!微乐小程序自建房辅助... 终于知道!微乐小程序自建房辅助!微乐小程序自建房透视下载(确实有辅助脚本)该软件可以轻松地帮助玩家将...
一分钟教会你!微乐小程序辅助工... 一分钟教会你!微乐小程序辅助工具!微乐小程序辅助开发(都是真的是有辅助工具)1、玩家可以在线上大神俱...
玩家必备攻略!微乐小程序自建房... 玩家必备攻略!微乐小程序自建房辅助!微信小程序微乐辅助器教程(真是是真的有辅助技巧)所有人都在同一条...
技巧知识分享!微乐小程序自建房... 技巧知识分享!微乐小程序自建房辅助!微乐斗地主有挂吗(好像是有辅助攻略)1、进入到是否有挂之后,能看...
推荐攻略!微乐小程序辅助工具!... 推荐攻略!微乐小程序辅助工具!微乐自建房辅助下载(总是真的有辅助软件)1、脚本辅助下载、免费透视脚本...
总算清楚!微乐小程序自建房辅助... 总算清楚!微乐小程序自建房辅助!微信小程序多乐辅助器(原来是真的有辅助器)透视方法中分为三种模型:靠...
教程攻略!微乐小程序自建房辅助... 教程攻略!微乐小程序自建房辅助!微信微乐游戏苹果辅助器(都是真的有辅助攻略)一、游戏安装教程牌型概率...