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:92003.创建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 注解

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.编写方法名
小提示 先写返回值类型 这样有提示

命名规则 Spring Data Commons - 参考文档
到此这篇关于SpringData整合ElasticSearch实现CRUD的示例代码(超详细)的文章就介绍到这了,更多相关SpringData ElasticSearch实现CRUD内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
java中计算字符串长度的方法及u4E00与u9FBB的认识
字符串采用unicode编码的方式时,计算字符串长度的方法找出UNICODE编码中的汉字的代表的范围“\u4E00” 到“\u9FBB”之间感兴趣的朋友可以参考本文,或许对你有所帮助2013-01-01
SpringBoot3.2.2整合MyBatis-Plus3.5.5依赖不兼容的问题解决
这篇文章给大家介绍了Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依赖不兼容问题,文中通过代码示例和图文介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下2024-01-01
Java中ByteArrayInputStream和ByteArrayOutputStream用法详解
这篇文章主要介绍了Java中ByteArrayInputStream和ByteArrayOutputStream用法详解, ByteArrayInputStream 的内部额外的定义了一个计数器,它被用来跟踪 read() 方法要读取的下一个字节2022-06-06


最新评论