当前位置: 首页 > news >正文

SpringDataElasticsearch在SpringBoot项目中的简单使用

1.引入Maven

在 Maven 中添加 Spring Data Elasticsearch 依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

2.配置Elasticsearch连接

配置 Elasticsearch 服务器地址和端口

spring:elasticsearch:uris: http://xx.xx.xx.xx:9200

3.创建JavaBean

创建一个代表 Elasticsearch 文档的 Java 类,并使用 Spring Data Elasticsearch 注解进行标注。

@Data
@Document(indexName = "blog")
public class Blog{@Idprivate Long blogId;@Field(type = FieldType.Text)private String title;@Field(type = FieldType.Text)private String content;@Field(type = FieldType.Text)private String author;//博客所属分类。@Field(type = FieldType.Keyword)private String category;//0: 未发布(草稿) 1:已发布 2:已删除@Field(type = FieldType.Integer)private int status;//序列号,用于给外部展示的id@Field(type = FieldType.Keyword)private String serialNum;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")@Field(type= FieldType.Date, format= DateFormat.date_hour_minute_second_fraction, pattern="yyyy-MM-dd HH:mm:ss.SSS")private Date createTime;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")@Field(type=FieldType.Date, format=DateFormat.date_hour_minute_second_fraction, pattern="yyyy-MM-dd HH:mm:ss.SSS")private Date updateTime;
}

4.创建 Repository 接口

创建一个继承自 ElasticsearchRepository 的接口,用于执行 CRUD 操作。例如,创建 BlogRepository:

public interface BlogRepository extends ElasticsearchRepository<Blog,String> {List<Blog> findByTitleIn(List<String> title);Page<Blog> findByTitleContaining(String title, Pageable pageable);
}

5. 使用 Repository 进行操作

在 Service 或 Controller 中注入 BlogRepository 并使用它执行查询、保存、删除等操作:

@Service
public class EsServiceImpl implements EsService {@Autowiredprivate BlogRepository blogRepository;@Overridepublic void getDetails(String title) throws Exception {List<String> list = new ArrayList<>();list.add(title);List<Blog> blog = blogRepository.findByTitleIn(list);}
}

6. 自定义查询方法

如果需要执行更复杂的查询,可以使用 @Query 注解在 Repository 方法上定义原生 Elasticsearch 查询 DSL:

@Repository
public interface BlogRepository extends ElasticsearchRepository<Blog,String> {List<Blog> findByTitleIn(List<String> title);@Query("{\"bool\":{\"must\":[{\"match\":{\"title\":\"?0\"}}]}}")List<Blog> findByTitle(String title);
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 这7款AI网站只用10分钟写万字论文,大学生救星来了吗?
  • 力扣:1456. 定长子串中元音的最大数目
  • 又又又掉毛季 满天飞的浮毛猫毛怎么解决?去浮毛空气净化器推荐
  • PaddleOCR 图片文字提取
  • Flask 异常处理
  • lvs、集群
  • 刷题DAY5
  • Openlayers6 图形绘制和修改功能(结合React)
  • 使用 MongoDB 构建 AI:Flagler Health 的 AI 旅程如何彻底改变患者护理
  • Canal单机部署
  • Python模拟退火算法
  • 一个Android下载网络图片显示并保存到系统相册的完整案例
  • 关于k8s的pvc存储卷
  • haproxy七层代理总结
  • SpringBoot参数校验详解
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • bootstrap创建登录注册页面
  • css属性的继承、初识值、计算值、当前值、应用值
  • DataBase in Android
  • HTTP中GET与POST的区别 99%的错误认识
  • input的行数自动增减
  • java 多线程基础, 我觉得还是有必要看看的
  • jquery ajax学习笔记
  • linux安装openssl、swoole等扩展的具体步骤
  • MySQL QA
  • Twitter赢在开放,三年创造奇迹
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • vue-router的history模式发布配置
  • 阿里云购买磁盘后挂载
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 记录一下第一次使用npm
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 云大使推广中的常见热门问题
  • 7行Python代码的人脸识别
  • 我们雇佣了一只大猴子...
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • #调用传感器数据_Flink使用函数之监控传感器温度上升提醒
  • #我与Java虚拟机的故事#连载18:JAVA成长之路
  • (1)虚拟机的安装与使用,linux系统安装
  • (BFS)hdoj2377-Bus Pass
  • (CVPRW,2024)可学习的提示:遥感领域小样本语义分割
  • (C语言)fread与fwrite详解
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (TipsTricks)用客户端模板精简JavaScript代码
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (篇九)MySQL常用内置函数
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (一)u-boot-nand.bin的下载
  • (转) Face-Resources
  • (转)Linux整合apache和tomcat构建Web服务器
  • (转)平衡树
  • (转)四层和七层负载均衡的区别