SpringData整合ElasticSearch實(shí)現(xiàn)CRUD的示例代碼(超詳細(xì))
1.導(dǎo)入依賴
<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.創(chuàng)建Bean
- @Document( indexName= xxx) ES 的索引名
- @Id ES 的文檔ID
- @Field ES的字段映射
- type = FieldType.Keyword 關(guān)鍵字 不分詞 ( ES中沒有String 只有 text (分詞) 和 keyword ( 不分詞 )
- index 是否能索引
- analyzer 使用的分詞器
- format 格式轉(zhuǎn)換 pattern 日期格式
- FieldType.Nested 集合屬性 避免查出業(yè)務(wù)錯(cuò)誤
@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 中能分詞的字段,這個(gè)字段數(shù)據(jù)類型必須是 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;
// 商品的熱度! 我們將商品被用戶點(diǎn)查看的次數(shù)越多,則說(shuō)明熱度就越高!
@Field(type = FieldType.Long)
private Long hotScore = 0L;
// 平臺(tái)屬性集合對(duì)象
// Nested 支持嵌套查詢
@Field(type = FieldType.Nested)
private List<SearchAttr> attrs;
}4.創(chuàng)建接口繼承 CrudRepository 接口
泛型1 : ES對(duì)應(yīng)的javaBean
泛型2 : 文檔唯一ID的類型
@Repository
public interface GoodsDao extends CrudRepository<Goods,Long> {
}注意 如果想實(shí)現(xiàn)分頁(yè) 請(qǐng)實(shí)現(xiàn) PagingAndSortingRepository 接口
@Repository
public interface GoodsDao extends PagingAndSortingRepository<Goods,Long> {
}接口上添加 @Repository 注解

5. 創(chuàng)建service 注入 接口代理類對(duì)象
@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.主啟動(dòng)類上添加 @EnableElasticsearchRepositories
@EnableElasticsearchRepositories
@SpringCloudApplication
public class EsListApp {
public static void main(String[] args) {
SpringApplication.run(EsListApp.class);
}
}7.編寫方法名
小提示 先寫返回值類型 這樣有提示

命名規(guī)則 Spring Data Commons - 參考文檔
到此這篇關(guān)于SpringData整合ElasticSearch實(shí)現(xiàn)CRUD的示例代碼(超詳細(xì))的文章就介紹到這了,更多相關(guān)SpringData ElasticSearch實(shí)現(xiàn)CRUD內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 動(dòng)態(tài)模擬操作系統(tǒng)進(jìn)程調(diào)度算法
這篇文章主要介紹了采用java語(yǔ)言編程模擬N個(gè)進(jìn)程采用動(dòng)態(tài)高優(yōu)先權(quán)優(yōu)先進(jìn)程調(diào)度算法。文中代碼具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2021-12-12
java中計(jì)算字符串長(zhǎng)度的方法及u4E00與u9FBB的認(rèn)識(shí)
字符串采用unicode編碼的方式時(shí),計(jì)算字符串長(zhǎng)度的方法找出UNICODE編碼中的漢字的代表的范圍“\u4E00” 到“\u9FBB”之間感興趣的朋友可以參考本文,或許對(duì)你有所幫助2013-01-01
SpringBoot3.2.2整合MyBatis-Plus3.5.5依賴不兼容的問(wèn)題解決
這篇文章給大家介紹了Spring Boot 3.2.2整合MyBatis-Plus 3.5.5依賴不兼容問(wèn)題,文中通過(guò)代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
Java中ByteArrayInputStream和ByteArrayOutputStream用法詳解
這篇文章主要介紹了Java中ByteArrayInputStream和ByteArrayOutputStream用法詳解,?ByteArrayInputStream?的內(nèi)部額外的定義了一個(gè)計(jì)數(shù)器,它被用來(lái)跟蹤?read()?方法要讀取的下一個(gè)字節(jié)2022-06-06

