Elastic连接SpringData

spring data 对很多nosql以及sql提供了统一的操作 spring data elastic

添加依赖

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

在yml文件中添加支持

spring:
  data:
    elasticsearch:
      cluster-nodes: 192.168.100.161:9200
      cluster-name: "elasticsearch"

添加对应实体类
@Document注解
indexName表示es中的索引 type表示所属的类型
对应于 Elasticsearch 的 Index 和Type

@Document(indexName = "product", type = "book")
@Data
public class BookEntity {
    @Id
    String id;
    String name;
    String massage;
    Date posDate;
    String type;
}

编写Repository
ElasticsearchRepository
简单的包含了增删改查,同理与jpa

public interface BookDao extends ElasticsearchRepository<BookEntity, String> {
    public List<BookEntity> getByMessage(String key);
}

es与spring data 的对应关系
这里写图片描述
如果包含翻页也可以使用page方法进行操控,同理与spring data jpa 的控制
## 尾声
到此为止es与springboot的简单整合就算是完成了,springboot目前对es的自动装配只支持到了2.x版本也就是说5.x以及6.xspringbooty都无法支持.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值