Java利用ElasticSearch實(shí)現(xiàn)增刪改功能
前言
最近在學(xué)習(xí)elasticsearch,所以從最簡(jiǎn)單的增刪改功能開始,下面是我的版本依賴,我使用的是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來實(shí)現(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)建時(shí)間
private Date updatedDt;//更新時(shí)間
}定義好之后,我們來實(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í)體類上添加@Document來指定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);
// 通過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í)體類上通過設(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)文章
idea maven 構(gòu)建本地jar包及pom文件的過程
這篇文章主要介紹了idea maven 構(gòu)建本地jar包及pom文件的過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
移動(dòng)開發(fā)Spring Boot外置tomcat教程及解決方法
這篇文章主要介紹了移動(dòng)開發(fā)SpringBoot外置tomcat教程,需要的朋友可以參考下2017-11-11
Java?BigDecimal與RoundingMode最常用的用法總結(jié)
RoundingMode是Java中定義舍入行為的枚舉類,包括UP、DOWN、CEILING等8種模式,適用于精確數(shù)字運(yùn)算,這篇文章主要介紹了Java?BigDecimal與RoundingMode最常用用法總結(jié)的相關(guān)資料,需要的朋友可以參考下2025-12-12
Linux(centos7)安裝jdk1.8的詳細(xì)步驟
Linux的使用相信大家都要用到j(luò)ava吧,在使用java前我們得先安裝jdk以及配置環(huán)境變量等工作,下面這篇文章主要給大家介紹了關(guān)于Linux(centos7)安裝jdk1.8的詳細(xì)步驟,需要的朋友可以參考下2023-10-10
Eclipse使用maven搭建spring mvc圖文教程
這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下2016-05-05
Spring實(shí)現(xiàn)Quartz自動(dòng)配置的方法詳解
這篇文章主要介紹了Spring實(shí)現(xiàn)Quartz自動(dòng)配置的方法詳解,如果想在應(yīng)用中使用Quartz任務(wù)調(diào)度功能,可以通過Spring Boot實(shí)現(xiàn)Quartz的自動(dòng)配置,以下介紹如何開啟Quartz自動(dòng)配置,以及Quartz自動(dòng)配置的實(shí)現(xiàn)過程,需要的朋友可以參考下2023-11-11

