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:9200
3.創(chuàng)建Bean
- @Document( indexName= xxx) ES 的索引名
- @Id ES 的文檔ID
- @Field ES的字段映射
- type = FieldType.Keyword 關(guān)鍵字 不分詞 ( ES中沒(méi)有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)文章
Spring AOP在web應(yīng)用中的使用方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12java在集合遍歷過(guò)程中刪除元素5種方法對(duì)比、案例、常見(jiàn)的錯(cuò)誤及其后果
這篇文章主要介紹了java在集合遍歷過(guò)程中刪除元素5種方法對(duì)比、案例、常見(jiàn)的錯(cuò)誤及其后果的相關(guān)資料,介紹了五種不同的解決方案,包括使用Iterator.remove()、for-each+手動(dòng)刪除、for循環(huán)反向遍歷、List.removeIf()和使用Stream.filter(),需要的朋友可以參考下2024-12-12Java五子棋簡(jiǎn)單實(shí)現(xiàn)代碼舉例
Java五子棋游戲是一種經(jīng)典的兩人對(duì)戰(zhàn)棋類游戲,它基于簡(jiǎn)單的規(guī)則,即任何一方的棋子在棋盤上形成連續(xù)的五個(gè),無(wú)論是橫、豎還是斜線,都將獲勝,這篇文章主要介紹了Java五子棋實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-10-10解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問(wèn)題
這篇文章主要介紹了解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11java可變參數(shù)(不定向參數(shù))的作用與實(shí)例
這篇文章主要給大家介紹了關(guān)于java可變參數(shù)(不定向參數(shù))的作用與實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04spring boot入門開(kāi)始你的第一個(gè)應(yīng)用
這篇文章主要介紹了spring boot入門開(kāi)始你的第一個(gè)應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06