Java利用ElasticSearch實現(xiàn)增刪改功能
前言
最近在學(xué)習(xí)elasticsearch,所以從最簡單的增刪改功能開始,下面是我的版本依賴,我使用的是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來實現(xiàn),先看看基礎(chǔ)類定義:
@Data
@Document(indexName = "index_urls", createIndex = true)
public class UrlIndex {
private String id;
private String uid;
private String title;
// 下拉提示建議使用的字段,注意:Completion類型字段,不能做篩選用
// @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)建時間
private Date updatedDt;//更新時間
}定義好之后,我們來實現(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);
動態(tài)index操作
如果我們不想在實體類上添加@Document來指定index,我們?nèi)绾螌崿F(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);
// 通過get查詢出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è)置@Document(indexName = "index_urls", createIndex = true)就可以實現(xiàn)索引就可以完成增刪改功能
2、但是如果你使用動態(tài)索引,則要指定你的索引名
3、我使用的是ElasticsearchOperations當(dāng)然還有其它的實現(xiàn)方式,如EnableElasticsearchRepositories
以上就是Java利用ElasticSearch實現(xiàn)增刪改功能的詳細(xì)內(nèi)容,更多關(guān)于Java ElasticSearch的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
servlet之web路徑問題_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了servlet之web路徑問題的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Java在Excel中添加水印的實現(xiàn)(單一水印、平鋪水印)
這篇文章主要介紹了Java在Excel中添加水印的實現(xiàn)(單一水印、平鋪水印),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
java volatile關(guān)鍵字作用及使用場景詳解
在本文里我們給大家分享的是關(guān)于java volatile關(guān)鍵字作用及使用場景的相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-08-08
Spring Boot集成spring-boot-devtools開發(fā)時實現(xiàn)熱部署的方式
這篇文章主要介紹了Spring Boot集成spring-boot-devtools開發(fā)時實現(xiàn)熱部署的方式,文中還給大家提到了spring boot 實現(xiàn)熱部署的方式及集成注意事項,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
為什么Spring和IDEA都不推薦使用 @Autowired 注解
本文主要介紹了為什么Spring和IDEA都不推薦使用 @Autowired 注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
在Spring-Boot中如何使用@Value注解注入集合類
這篇文章主要介紹了在Spring-Boot中如何使用@Value注解注入集合類的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08

