SpringBoot整合Easy-ES操作演示文檔
1 概述及特性
1.1 官網(wǎng)
- Easy-ES官網(wǎng): https://www.easy-es.cn/
- 官方示例: https://gitee.com/dromara/easy-es/tree/master/easy-es-sample
- 參考鏈接: https://blog.51cto.com/yueshushu/6193710
1.2 主要特性
- **零侵入:**針對ES官方提供的RestHighLevelClient只做增強(qiáng)不做改變,引入EE不會對現(xiàn)有工程產(chǎn)生影響,使用體驗(yàn)如絲般順滑。
- **損耗?。?*啟動(dòng)即會自動(dòng)注入基本 CURD,性能基本無損耗,直接面向?qū)ο蟛僮鳌?/li>
- 自動(dòng)化: 全球領(lǐng)先的哥哥你不用動(dòng),索引我全自動(dòng)模式,幫助開發(fā)者和運(yùn)維杜絕索引困擾。
- 智能化: 根據(jù)索引類型和當(dāng)前查詢類型上下文綜合智能判斷當(dāng)前查詢是否需要拼接.keyword后綴,減少小白誤用的可能。
- **強(qiáng)大的 CRUD 操作:*內(nèi)置通用 Mapper,僅僅通過少量配置即可實(shí)現(xiàn)大部分 CRUD 操作,更 有強(qiáng)大的條件構(gòu)造器,滿足各類使用需求。
- **支持 Lambda 形式調(diào)用:**通過 Lambda 表達(dá)式,方便的編寫各類查詢條件,無需再擔(dān)心字段寫錯(cuò)段。
- **支持主鍵自動(dòng)生成:**支持多種主鍵策略,可自由配置,完美解決主鍵問題。
- **支持 ActiveRecord 模式:**支持 ActiveRecord 形式調(diào)用,實(shí)體類只需繼承 Model 類即可進(jìn)行強(qiáng)大的 CRUD 操作。
- **支持自定義全局通用操作:**支持全局通用方法注入( Write once, use anywhere )。
- **內(nèi)置分頁插件:**基于RestHighLevelClient 物理分頁,開發(fā)者無需關(guān)心具體操作,且無需額外配置插件,寫分頁等同于普通 List 查詢,比MP的PageHelper插件用起來更簡單,且保持與其同樣的分頁返回字段,無需擔(dān)心命名影響。
- **MySQL功能全覆蓋:**MySQL中支持的功能通過EE都可以輕松實(shí)現(xiàn)。
- **支持ES高階語法:**支持聚合,嵌套,父子類型,高亮搜索,分詞查詢,權(quán)重查詢,Geo地理位置查詢,IP查詢等高階語法,應(yīng)有盡有。
- **良好的拓展性:*底層仍使用RestHighLevelClient,可保持其拓展性,開發(fā)者在使用EE的同時(shí), * 仍可使用RestHighLevelClient的所有功能。
2 整合配置
2.1 導(dǎo)入POM
- Latest Version: 2.0.0-beta4
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>${Latest Version}</version>
</dependency>
2.2 Yaml配置
easy-es:
# 基礎(chǔ)配置項(xiàng)
enable: true
address: 10.15.20.11:9200
schema: http
username:
password:
keep-alive-millis: 18000
# 擴(kuò)展的連接池配置項(xiàng)
global-config:
process-index-mode: smoothly
async-process-index-blocking: true
print-dsl: true
db-config:
map-underscore-to-camel-case: true
id-type: customize
field-strategy: not_empty
refresh-policy: immediate
enable-track-total-hits: true
2.3 @EsMapperScan 注解掃描
- 標(biāo)注與主啟動(dòng)類上,功能與MP的@MapperScan一致。
package com.xs.easy;
import org.dromara.easyes.starter.register.EsMapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EsMapperScan("com.xs.easy.mapper")
@EnableConfigurationProperties
@SpringBootApplication
public class XsEasyApplication {
public static void main(String[] args) {
SpringApplication.run(XsEasyApplication.class, args);
}
}
2.4 配置Entity
package com.xs.easy.entity;
import lombok.Data;
import lombok.experimental.Accessors;
import org.dromara.easyes.annotation.HighLight;
import org.dromara.easyes.annotation.IndexField;
import org.dromara.easyes.annotation.IndexId;
import org.dromara.easyes.annotation.IndexName;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldStrategy;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.annotation.rely.IdType;
/**
* es 數(shù)據(jù)模型
**/
@Data
@Accessors(chain = true)
@IndexName(value = "easy-es-document", shardsNum = 3, replicasNum = 2, keepGlobalPrefix = true, maxResultWindow = 100)
public class Document {
/**
* es中的唯一id,如果你想自定義es中的id為你提供的id,比如MySQL中的id,請將注解中的type指定為customize或直接在全局配置文件中指定,如此id便支持任意數(shù)據(jù)類型)
*/
@IndexId(type = IdType.CUSTOMIZE)
private String id;
/**
* 文檔標(biāo)題,不指定類型默認(rèn)被創(chuàng)建為keyword類型,可進(jìn)行精確查詢
*/
private String title;
/**
* 文檔內(nèi)容,指定了類型及存儲/查詢分詞器
*/
@HighLight(mappingField = "highlightContent")
@IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART)
private String content;
/**
* 作者 加@TableField注解,并指明strategy = FieldStrategy.NOT_EMPTY 表示更新的時(shí)候的策略為 創(chuàng)建者不為空字符串時(shí)才更新
*/
@IndexField(strategy = FieldStrategy.NOT_EMPTY)
private String creator;
/**
* 創(chuàng)建時(shí)間
*/
@IndexField(fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")
private String gmtCreate;
/**
* es中實(shí)際不存在的字段,但模型中加了,為了不和es映射,可以在此類型字段上加上 注解@TableField,并指明exist=false
*/
@IndexField(exist = false)
private String notExistsField;
/**
* 地理位置經(jīng)緯度坐標(biāo) 例如: "40.13933715136454,116.63441990026217"
*/
@IndexField(fieldType = FieldType.GEO_POINT)
private String location;
/**
* 圖形(例如圓心,矩形)
*/
@IndexField(fieldType = FieldType.GEO_SHAPE)
private String geoLocation;
/**
* 自定義字段名稱
*/
@IndexField(value = "wu-la", fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_SMART, fieldData = true)
private String customField;
/**
* 高亮返回值被映射的字段
*/
private String highlightContent;
/**
* 文檔點(diǎn)贊數(shù)
*/
private Integer starNum;
}
2.5 配置Mapper
package com.xs.easy.mapper;
import com.xs.easy.entity.Document;
import org.dromara.easyes.core.core.BaseEsMapper;
/**
* mapper 相當(dāng)于Mybatis-plus的mapper
**/
public interface DocumentMapper extends BaseEsMapper<Document> {
}
3 基礎(chǔ)操作
3.1 批量保存
public Integer insertES(int num) {
List<Document> lis = new ArrayList<>();
for (int i = 0; i < num; i++) {
Document document = new Document();
document.setId(ChineseUtil.randomNumber(1, 1000000000) + "");
document.setTitle(ChineseRandomGeneration.GBKMethod(16));
document.setContent(ChineseRandomGeneration.GBKMethod(160));
document.setCreator(ChineseUtil.randomChineseName());
document.setGmtCreate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
document.setStarNum(ChineseUtil.randomNumber(1, 10000));
lis.add(document);
}
return this.documentMapper.insertBatch(lis);
}
3.2 數(shù)據(jù)更新
public Integer updateES(Document doc) {
return this.documentMapper.updateById(doc);
}
3.3 數(shù)據(jù)刪除
public Integer removeES(String id) {
// 條件構(gòu)造
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.eq(Document::getId, id);
return documentMapper.delete(wrapper);
}
3.4 組合查詢
public List<Document> listByES(Document doc) {
// 條件構(gòu)造
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.like(StringUtils.isNotBlank(doc.getTitle()), Document::getTitle, doc.getTitle());
wrapper.like(StringUtils.isNotBlank(doc.getContent()), Document::getTitle, doc.getContent());
return documentMapper.selectList(wrapper);
}
3.5 高亮查詢
public List<Document> highSearchES(Document doc) {
// 條件構(gòu)造
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
wrapper.match(StringUtils.isNotBlank(doc.getContent()), Document::getContent, doc.getContent());
return documentMapper.selectList(wrapper);
}
3.6 統(tǒng)計(jì)查詢
public Long countTotal() {
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
return documentMapper.selectCount(wrapper);
}
4 整合異常
4.1 XContentType找不到問題
- java.lang.NoClassDefFoundError: org/elasticsearch/common/xcontent/XContentType
文章來源:http://www.zghlxwxcb.cn/news/detail-722897.html
- 排除Easy-Es中的依賴
<!-- Easy-ES -->
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<!--排除依賴-->
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
- 重新引入指定版本的組件
<properties>
<es.version>7.10.1</es.version>
<es-rest-high-level-client.version>7.10.1</es-rest-high-level-client.version>
</properties>
<!--新增依賴-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${es-rest-high-level-client.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
</dependency>
文章來源地址http://www.zghlxwxcb.cn/news/detail-722897.html
到了這里,關(guān)于SpringBoot整合Easy-ES操作演示文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!