Java利用ElasticSearch實(shí)現(xiàn)增刪改功能
前言
最近在學(xué)習(xí)elasticsearch,所以從最簡(jiǎn)單的增刪改功能開(kāi)始,下面是我的版本依賴(lài),我使用的是java17
、elasticsearch-java8.7
和spring-boot3.0
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.7.1</version> </dependency>
靜態(tài)index操作
我使用的是ElasticsearchOperations
來(lái)實(shí)現(xiàn),先看看基礎(chǔ)類(lèi)定義:
@Data @Document(indexName = "index_urls", createIndex = true) public class UrlIndex { private String id; private String uid; private String title; // 下拉提示建議使用的字段,注意:Completion類(lèi)型字段,不能做篩選用 // @CompletionField(analyzer="ik_smart",searchAnalyzer="ik_smart", maxInputLength = 100) private Completion suggest; private String url; private String domain; private String description; private String favicon; private Date createdDt;//創(chuàng)建時(shí)間 private Date updatedDt;//更新時(shí)間 }
定義好之后,我們來(lái)實(shí)現(xiàn)增刪改功能:
新增:
UrlIndex url = JSONObject.parseObject(msg, UrlIndex.class); UrlIndex urlIndex = elasticsearchOperations.save(url);
編輯:
Url url = JSONObject.parseObject(msg, Url.class); UrlIndex urlIndex = elasticsearchOperations.get(url.getId(), UrlIndex.class); if (urlIndex != null) { urlIndex.setTitle(url.getTitle()); // 存在即更新,,注意要設(shè)置index elasticsearchOperations.save(urlIndex); }
刪除:
elasticsearchOperations.delete(id, UrlIndex.class);
動(dòng)態(tài)index操作
如果我們不想在實(shí)體類(lèi)上添加@Document
來(lái)指定index,我們?nèi)绾螌?shí)現(xiàn)呢?
@Document(indexName = "index_urls", createIndex = true)
新增:
UrlIndex url = JSONObject.parseObject(msg, UrlIndex.class); IndexCoordinates indexCoordinates = IndexCoordinates.of("index_name"); UrlIndex urlIndex = elasticsearchOperations.save(url, indexCoordinates);
編輯:
IndexCoordinates indexCoordinates = IndexCoordinates.of("index_name"); UrlIndex resUrl = JSONObject.parseObject(msg, UrlIndex.class); // 通過(guò)get查詢(xún)出es數(shù)據(jù),注意要設(shè)置index UrlIndex urlIndex = elasticsearchOperations.get(resUrl.getId(), UrlIndex.class, indexCoordinates); if (urlIndex != null) { urlIndex.setTitle(resUrl.getTitle()); urlIndex.setSuggest(resUrl.getSuggest()); // 存在即更新,,注意要設(shè)置index elasticsearchOperations.save(urlIndex, indexCoordinates); }
刪除:
IndexCoordinates indexCoordinates = IndexCoordinates.of("index_name"); elasticsearchOperations.delete(msg, indexCoordinates);
總結(jié)
1、在實(shí)體類(lèi)上通過(guò)設(shè)置@Document(indexName = "index_urls", createIndex = true)
就可以實(shí)現(xiàn)索引就可以完成增刪改功能
2、但是如果你使用動(dòng)態(tài)索引,則要指定你的索引名
3、我使用的是ElasticsearchOperations
當(dāng)然還有其它的實(shí)現(xiàn)方式,如EnableElasticsearchRepositories
以上就是Java利用ElasticSearch實(shí)現(xiàn)增刪改功能的詳細(xì)內(nèi)容,更多關(guān)于Java ElasticSearch的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java如何讀取resources目錄和同級(jí)目錄文件
介紹了Java中讀取resources目錄和同級(jí)目錄文件的方法,并討論了在IDE和發(fā)布環(huán)境中可能遇到的問(wèn)題,通過(guò)測(cè)試發(fā)現(xiàn),執(zhí)行目錄可能會(huì)影響文件讀取,建議在使用`user.dir`時(shí)注意jar包的運(yùn)行目錄問(wèn)題2024-12-12servlet之web路徑問(wèn)題_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了servlet之web路徑問(wèn)題的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印)
這篇文章主要介紹了Java在Excel中添加水印的實(shí)現(xiàn)(單一水印、平鋪水印),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04java volatile關(guān)鍵字作用及使用場(chǎng)景詳解
在本文里我們給大家分享的是關(guān)于java volatile關(guān)鍵字作用及使用場(chǎng)景的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-08-08Spring Boot集成spring-boot-devtools開(kāi)發(fā)時(shí)實(shí)現(xiàn)熱部署的方式
這篇文章主要介紹了Spring Boot集成spring-boot-devtools開(kāi)發(fā)時(shí)實(shí)現(xiàn)熱部署的方式,文中還給大家提到了spring boot 實(shí)現(xiàn)熱部署的方式及集成注意事項(xiàng),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05為什么Spring和IDEA都不推薦使用 @Autowired 注解
本文主要介紹了為什么Spring和IDEA都不推薦使用 @Autowired 注解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04java: 錯(cuò)誤: 無(wú)效的源發(fā)行版18問(wèn)題及解決
這篇文章主要介紹了java: 錯(cuò)誤: 無(wú)效的源發(fā)行版18問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04在Spring-Boot中如何使用@Value注解注入集合類(lèi)
這篇文章主要介紹了在Spring-Boot中如何使用@Value注解注入集合類(lèi)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08