SpringData整合ElasticSearch实现CRUD的示例代码(超详细)

 更新时间:2023年07月21日 11:54:47   作者:秋日的晚霞  
本文主要介绍了SpringData整合ElasticSearch实现CRUD的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.导入依赖

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

2.配置 yml

spring:
  elasticsearch:
    rest:
      uris:
        - http://xxxxx:9200

3.创建Bean

  • @Document( indexName= xxx) ES 的索引名
  • @Id ES 的文档ID
  • @Field ES的字段映射
  • type = FieldType.Keyword 关键字 不分词 ( ES中没有String 只有 text (分词) 和 keyword ( 不分词 )
  • index 是否能索引
  • analyzer 使用的分词器
  • format 格式转换 pattern 日期格式
  • FieldType.Nested 集合属性 避免查出业务错误
@Document(indexName = "goods" , shards = 3,replicas = 2)
public class Goods {
    // 商品Id skuId
    @Id
    private Long id;
    @Field(type = FieldType.Keyword, index = false)
    private String defaultImg;
    //  es 中能分词的字段,这个字段数据类型必须是 text!keyword 不分词!
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;
    @Field(type = FieldType.Double)
    private Double price;
    //  @Field(type = FieldType.Date)   6.8.1
    @Field(type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime; // 新品
    @Field(type = FieldType.Long)
    private Long tmId;
    @Field(type = FieldType.Keyword)
    private String tmName;
    @Field(type = FieldType.Keyword)
    private String tmLogoUrl;
    @Field(type = FieldType.Long)
    private Long category1Id;
    @Field(type = FieldType.Keyword)
    private String category1Name;
    @Field(type = FieldType.Long)
    private Long category2Id;
    @Field(type = FieldType.Keyword)
    private String category2Name;
    @Field(type = FieldType.Long)
    private Long category3Id;
    @Field(type = FieldType.Keyword)
    private String category3Name;
    //  商品的热度! 我们将商品被用户点查看的次数越多,则说明热度就越高!
    @Field(type = FieldType.Long)
    private Long hotScore = 0L;
    // 平台属性集合对象
    // Nested 支持嵌套查询
    @Field(type = FieldType.Nested)
    private List<SearchAttr> attrs;
}

4.创建接口继承 CrudRepository 接口

泛型1 : ES对应的javaBean

泛型2 : 文档唯一ID的类型

@Repository
public interface GoodsDao extends CrudRepository<Goods,Long> {
}

注意 如果想实现分页 请实现 PagingAndSortingRepository 接口

@Repository
public interface GoodsDao extends PagingAndSortingRepository<Goods,Long> {
}

接口上添加 @Repository 注解

image-20220314200112925

5. 创建service 注入 接口代理类对象

@Service
public class GoodsServiceImpl implements GoodService {
    @Autowired
    private GoodsDao goodsDao;
    @Override
    public boolean onSale(Goods goods) {
        Goods save = goodsDao.save(goods);
       return !StringUtils.isEmpty(save);
    }
}

6.主启动类上添加 @EnableElasticsearchRepositories

@EnableElasticsearchRepositories
@SpringCloudApplication
public class EsListApp {
    public static void main(String[] args) {
        SpringApplication.run(EsListApp.class);
    }
}

7.编写方法名

小提示 先写返回值类型 这样有提示

idea64_vJZvPkhh48

命名规则 Spring Data Commons - 参考文档

到此这篇关于SpringData整合ElasticSearch实现CRUD的示例代码(超详细)的文章就介绍到这了,更多相关SpringData ElasticSearch实现CRUD内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java数学工具类Math详解(round方法)

    java数学工具类Math详解(round方法)

    这篇文章主要为大家详细介绍了java数学工具类Math,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • java实现Redisson的基本使用

    java实现Redisson的基本使用

    Redisson是一个在Redis的基础上实现的Java驻内存数据网格客户端,本文主要介绍了java实现Redisson的基本使用,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • java实现模拟USB接口的功能

    java实现模拟USB接口的功能

    本文主要介绍了java实现模拟USB接口的功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • java中初始化MediaRecorder的实现方法

    java中初始化MediaRecorder的实现方法

    这篇文章主要介绍了java中初始化MediaRecorder的实现方法的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下
    2017-10-10
  • 关于maven的用法和几个常用的命令

    关于maven的用法和几个常用的命令

    这篇文章主要介绍了关于maven的用法和几个常用的命令,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • 利用javaFX实现移动一个小球的示例代码

    利用javaFX实现移动一个小球的示例代码

    这篇文章主要介绍了利用javaFX实现移动一个小球的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • SpringSecurity权限控制实现原理解析

    SpringSecurity权限控制实现原理解析

    这篇文章主要介绍了SpringSecurity权限控制实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java 多线程同步 锁机制与synchronized深入解析

    Java 多线程同步 锁机制与synchronized深入解析

    从尺寸上讲,同步代码块比同步方法小。你可以把同步代码块看成是没上锁房间里的一块用带锁的屏风隔开的空间
    2013-09-09
  • mybatis的坑-integer类型为0的数据if test失效问题

    mybatis的坑-integer类型为0的数据if test失效问题

    这篇文章主要介绍了mybatis的坑-integer类型为0的数据if test失效问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Java NIO 中 Selector 解析

    Java NIO 中 Selector 解析

    这篇文章主要介绍了Java NIO 中 Selector,Selector即选择器,选择器提供选择执行已经就绪的任务的能力即为翻译为多路复用,下面文章对Selector详细介绍内容,需要的小伙伴可以参考一下
    2022-02-02

最新评论