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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring AOP在web应用中的使用方法实例

    Spring AOP在web应用中的使用方法实例

    这篇文章主要给大家介绍了关于Spring AOP在web应用中的使用方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring AOP具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-12-12
  • java在集合遍历过程中删除元素5种方法对比、案例、常见的错误及其后果

    java在集合遍历过程中删除元素5种方法对比、案例、常见的错误及其后果

    这篇文章主要介绍了java在集合遍历过程中删除元素5种方法对比、案例、常见的错误及其后果的相关资料,介绍了五种不同的解决方案,包括使用Iterator.remove()、for-each+手动删除、for循环反向遍历、List.removeIf()和使用Stream.filter(),需要的朋友可以参考下
    2024-12-12
  • Java五子棋简单实现代码举例

    Java五子棋简单实现代码举例

    Java五子棋游戏是一种经典的两人对战棋类游戏,它基于简单的规则,即任何一方的棋子在棋盘上形成连续的五个,无论是横、竖还是斜线,都将获胜,这篇文章主要介绍了Java五子棋实现的相关资料,需要的朋友可以参考下
    2024-10-10
  • Java中日期与时间的处理及工具类封装详解

    Java中日期与时间的处理及工具类封装详解

    在项目开发中免不了有对日期时间的处理,但Java中关于日期时间的类太多了,本文就来介绍一下各种类的使用及我们项目中应该怎么选择吧
    2023-07-07
  • 解决Maven中关于依赖导入不进的问题

    解决Maven中关于依赖导入不进的问题

    这篇文章主要介绍了解决Maven中关于依赖导入不进的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • Spring整合消息队列RabbitMQ流程

    Spring整合消息队列RabbitMQ流程

    Spring整合RabbitMQ很容易,但是整合的目的是为了使用,那要使用RabbitMQ就要对其有一定的了解,不然容易整成一团浆糊。因为说到底,Spring只是在封装RabbitMQ的API,让其更容易使用而已,废话不多说,让我们一起整它
    2023-03-03
  • java可变参数(不定向参数)的作用与实例

    java可变参数(不定向参数)的作用与实例

    这篇文章主要给大家介绍了关于java可变参数(不定向参数)的作用与实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Struts2框架初学接触

    Struts2框架初学接触

    本文主要给大家从初学者的角度介绍了Struts2框架结构和基本页面代码等内容,一起来学习一下。
    2017-11-11
  • spring boot入门开始你的第一个应用

    spring boot入门开始你的第一个应用

    这篇文章主要介绍了spring boot入门开始你的第一个应用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
    2019-06-06
  • 关于Java日期工具类的编写

    关于Java日期工具类的编写

    这篇文章主要介绍了关于Java日期工具类的编写,在Java开发中,经常会遇到处理日期相关的数据,那么今天我们来自己写一个工具类,文中有详细的实例代码以及实现思路,需要的朋友可以参考下
    2023-05-05

最新评论