springboot如何整合elasticsearch
前言
推薦首先查看spring boot對(duì)應(yīng)elasticsearch版本,選擇合適的版本整合,推薦以spring boot版本為主,因?yàn)轫?xiàng)目中集成的框不止是es,根據(jù)spring boot去安裝對(duì)應(yīng)版本的es。
Spring Data Elasticsearch - 參考文檔,這是官方文檔,建議一定參照文檔,這個(gè)文檔真的很詳細(xì)。

另外,springboot操作elasticsearch有兩種常用方式:
不管使用哪一種,文章開(kāi)頭的參考文檔地址里邊都有詳細(xì)介紹,可以下載一個(gè)瀏覽器翻譯插件,這樣看起來(lái)更輕松。

Spring Data Elasticsearch
這是Spring官方最推薦的,就像JPA,Mybatisplus一樣,在DAO層繼承ElasticsearchRepository接口,就可以使用封裝好的一些常見(jiàn)的操作了,用起來(lái)簡(jiǎn)單方便。

ElasticsearchRestTemplate
封裝的就是High Level REST Client,這是基于HTTP協(xié)議的客戶(hù)端,是ES官方推薦使用的,也是可以使用的,但是要求對(duì)ES的DSL語(yǔ)句熟悉,方便自己做復(fù)雜的增刪改查。

不同方式演示
首先需要搞清楚映射關(guān)系,參考官方文檔這部分,內(nèi)容過(guò)多,就不一一寫(xiě)了。

簡(jiǎn)單看一下
注解:@Document用來(lái)聲明Java對(duì)象與ElasticSearch索引的關(guān)系
indexName索引名稱(chēng)(是字母的話(huà)必須是小寫(xiě)字母)type索引類(lèi)型shards主分區(qū)數(shù)量,默認(rèn)5replicas副本分區(qū)數(shù)量,默認(rèn)1createIndex索引不存在時(shí),是否自動(dòng)創(chuàng)建索引,默認(rèn)true 不建議自動(dòng)創(chuàng)建索引(自動(dòng)創(chuàng)建的索引 是按著默認(rèn)類(lèi)型和默認(rèn)分詞器)
注解:@Id 表示索引的主鍵
注解:@Field 用來(lái)描述字段的ES數(shù)據(jù)類(lèi)型,是否分詞等配置,等于Mapping描述
index設(shè)置字段是否索引,默認(rèn)是true,如果是false則該字段不能被查詢(xún)store標(biāo)記原始字段值是否應(yīng)該存儲(chǔ)在 Elasticsearch 中,默認(rèn)值為false,以便于快速檢索。雖然store占用磁盤(pán)空間,但是減少了計(jì)算。type數(shù)據(jù)類(lèi)型(text、keyword、date、object、geo等)analyzer對(duì)字段使用分詞器,注意一般如果要使用分詞器,字段的type一般是text。format定義日期時(shí)間格式
注解:@CompletionField 定義關(guān)鍵詞索引 要完成補(bǔ)全搜索
analyzer對(duì)字段使用分詞器,注意一般如果要使用分詞器,字段的type一般是text。searchAnalyzer顯示指定搜索時(shí)分詞器,默認(rèn)是和索引是同一個(gè),保證分詞的一致性。maxInputLength設(shè)置單個(gè)輸入的長(zhǎng)度,默認(rèn)為50 UTF-16 代碼點(diǎn)
集成先決配置
依賴(lài)包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
yml簡(jiǎn)單配置
server:
port: 8082
spring:
elasticsearch:
rest:
uris: 192.168.25.131:9200
實(shí)體類(lèi)
@Data
@AllArgsConstructor
@NoArgsConstructor
//indexName名字如果是字母那么必須是小寫(xiě)字母
@Document(indexName = "student")
public class Student {
@Id
@Field(store = true, type = FieldType.Keyword)
private String sId;
@Field(store = true, type = FieldType.Keyword)
private String sName;
@Field(store = true, type = FieldType.Text, analyzer = "ik_smart")
//Text可以分詞 ik_smart=粗粒度分詞 ik_max_word 為細(xì)粒度分詞
private String sAddress;
@Field(index = false, store = true, type = FieldType.Integer)
private Integer sAge;
@Field(index = false, store = true, type = FieldType.Date, format = DateFormat.basic_date_time)
private Date sCreateTime;
@Field(type = FieldType.Keyword)
private String[] sCourseList; //數(shù)組類(lèi)型 由數(shù)組中第一個(gè)非空值決定(這里數(shù)組和集合一個(gè)意思了)
@Field(type = FieldType.Keyword)
private List<String> sColorList; //集合類(lèi)型 由數(shù)組中第一個(gè)非空值決定
}
Spring Data Elasticsearch方式
先看文檔了解一下定義接口方法的規(guī)則吧,前邊說(shuō)過(guò),這種方式就是類(lèi)似JPA和Mybatisplus的方式,所以不難理解哈。
定義mapper
/**
* @author: zhouwenjie
* @description:
* @create: 2022-05-12 17:37
* ElasticsearchRepository<T, ID> T:實(shí)體類(lèi)泛型,ID:實(shí)體類(lèi)主鍵類(lèi)型
**/
public interface StudentMapper extends ElasticsearchRepository<Student, String> {
}
使用es自帶的一些增刪改查方法
如下圖,可以看到ElasticsearchRepository本身自帶了一些簡(jiǎn)單curd方法。

測(cè)試
@Resource
StudentMapper studentMapper;
@Test
void contextLoads3() {
Optional<Student> optionalStudent = studentMapper.findById("2");
System.out.println(optionalStudent.get());
}
使用自定義的方法
規(guī)則參考官網(wǎng)的這部分

自定義方法
/**
* @author: zhouwenjie
* @description:
* @create: 2022-05-12 17:37
* ElasticsearchRepository<T, ID> T:實(shí)體類(lèi)泛型,ID:實(shí)體類(lèi)主鍵類(lèi)型
**/
public interface StudentMapper extends ElasticsearchRepository<Student, String> {
//提示方法名SName,但是s是小寫(xiě)sName才可以
List<Student> findStudentBysName(String name);
}
測(cè)試
@Test
void contextLoads3() {
List<Student> students = studentMapper.findStudentBysName("fff");
System.out.println(students);
}
好了,測(cè)試到此為止,更多需求可以參照官方文檔自行實(shí)現(xiàn)。
ElasticsearchRestTemplate方式
返回結(jié)果,參照官方說(shuō)明:

添加
@Test
void contextLoads2() {
List<String> colorList = new ArrayList<>();//顏色
colorList.add("red");
colorList.add("white");
colorList.add("black");
Student student = new Student("1", "mhh", "濟(jì)南", 12, new Date(), new String[]{"語(yǔ)文", "數(shù)學(xué)", "英語(yǔ)"}, colorList);
Student save = restTemplate.save(student);
System.out.println(save);
}

查詢(xún)
@Test
void contextLoads() {
Criteria criteria = new Criteria("sName").is("mhh").and("sAddress").is("濟(jì)南");
Query query = new CriteriaQuery(criteria);
SearchHits<Student> mapSearchHits = restTemplate.search(query, Student.class, IndexCoordinates.of("student"));
List<SearchHit<Student>> searchHits = mapSearchHits.getSearchHits();
for (SearchHit<Student> searchHit : searchHits) {
Student student = searchHit.getContent();
System.out.println(student);
}
}
更新
@Test
void contextLoads2() {
HashMap<String, Object> map = new HashMap<>();
map.put("sName","fff");
UpdateQuery.Builder builder = UpdateQuery.builder("1").withDocument(Document.from(map));
UpdateResponse update = restTemplate.update(builder.build(), IndexCoordinates.of("student"));
System.out.println(update);
}

刪除
@Test
void contextLoads2() {
String delete = restTemplate.delete("1",IndexCoordinates.of("student"));
System.out.println(delete);
}
這些演示都是最簡(jiǎn)單的,根據(jù)實(shí)際情況推薦大家去官網(wǎng)查詢(xún)更多復(fù)雜用法。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring集成Druid連接池及監(jiān)控配置的全過(guò)程
java程序很大一部分要操作數(shù)據(jù)庫(kù),為了提高性能操作數(shù)據(jù)庫(kù)的時(shí)候,有不得不使用數(shù)據(jù)庫(kù)連接池,下面這篇文章主要給大家介紹了關(guān)于Spring集成Druid連接池及監(jiān)控配置的相關(guān)資料,需要的朋友可以參考下2021-09-09
解析SpringBoot?搭建基于?MinIO?的高性能存儲(chǔ)服務(wù)的問(wèn)題
Minio是Apache?License?v2.0下發(fā)布的對(duì)象存儲(chǔ)服務(wù)器,使用MinIO構(gòu)建用于機(jī)器學(xué)習(xí),分析和應(yīng)用程序數(shù)據(jù)工作負(fù)載的高性能基礎(chǔ)架構(gòu)。這篇文章主要介紹了SpringBoot?搭建基于?MinIO?的高性能存儲(chǔ)服務(wù),需要的朋友可以參考下2022-03-03
SpringMVC通過(guò)注解獲得參數(shù)的實(shí)例
下面小編就為大家?guī)?lái)一篇SpringMVC通過(guò)注解獲得參數(shù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
IDEA實(shí)用好用插件推薦及使用方法教程詳解(必看)
這篇文章主要介紹了IDEA實(shí)用好用插件推薦及使用方法教程,本文通過(guò)實(shí)例截圖相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
使用logback實(shí)現(xiàn)按自己的需求打印日志到自定義的文件里
這篇文章主要介紹了使用logback實(shí)現(xiàn)按自己的需求打印日志到自定義的文件里,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳
這篇文章主要介紹了Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11
解決spring中redistemplate不能用通配符keys查出相應(yīng)Key的問(wèn)題
這篇文章主要介紹了解決spring中redistemplate不能用通配符keys查出相應(yīng)Key的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼
這篇文章主要介紹了JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼,涉及Java日期操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

