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中沒有String 只有 text (分詞) 和 keyword ( 不分詞 )
- index 是否能索引
- analyzer 使用的分詞器
- format 格式轉(zhuǎn)換 pattern 日期格式
- FieldType.Nested 集合屬性 避免查出業(yè)務(wù)錯誤
@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 中能分詞的字段,這個字段數(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ù)越多,則說明熱度就越高! @Field(type = FieldType.Long) private Long hotScore = 0L; // 平臺屬性集合對象 // Nested 支持嵌套查詢 @Field(type = FieldType.Nested) private List<SearchAttr> attrs; }
4.創(chuàng)建接口繼承 CrudRepository 接口
泛型1 : ES對應(yīng)的javaBean
泛型2 : 文檔唯一ID的類型
@Repository public interface GoodsDao extends CrudRepository<Goods,Long> { }
注意 如果想實(shí)現(xiàn)分頁 請實(shí)現(xiàn) PagingAndSortingRepository 接口
@Repository public interface GoodsDao extends PagingAndSortingRepository<Goods,Long> { }
接口上添加 @Repository 注解
5. 創(chuàng)建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.編寫方法名
小提示 先寫返回值類型 這樣有提示
命名規(guī)則 Spring Data Commons - 參考文檔
到此這篇關(guān)于SpringData整合ElasticSearch實(shí)現(xiàn)CRUD的示例代碼(超詳細(xì))的文章就介紹到這了,更多相關(guān)SpringData ElasticSearch實(shí)現(xiàn)CRUD內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring AOP在web應(yīng)用中的使用方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring AOP在web應(yīng)用中的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring AOP具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12java在集合遍歷過程中刪除元素5種方法對比、案例、常見的錯誤及其后果
這篇文章主要介紹了java在集合遍歷過程中刪除元素5種方法對比、案例、常見的錯誤及其后果的相關(guān)資料,介紹了五種不同的解決方案,包括使用Iterator.remove()、for-each+手動刪除、for循環(huán)反向遍歷、List.removeIf()和使用Stream.filter(),需要的朋友可以參考下2024-12-12解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問題
這篇文章主要介紹了解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11java可變參數(shù)(不定向參數(shù))的作用與實(shí)例
這篇文章主要給大家介紹了關(guān)于java可變參數(shù)(不定向參數(shù))的作用與實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04