添加依赖
<!-- 引入easy-es最新版本的依赖-->
<dependency>
<groupId>org.dromara.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<!--这里Latest Version是指最新版本的依赖,比如2.0.0,可以通过下面的图片获取-->
<version>2.0.0-beta3</version>
</dependency>
<!-- 排除springboot中内置的es依赖,以防和easy-es中的依赖冲突-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.14.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.14.0</version>
</dependency>
添加配置
easy-es:
enable: true #默认为true,若为false则认为不启用本框架
address: ip:port
schema: http # 默认值为http 可缺省 也支持https免ssl方式 配置此值为 https 即可
banner: true # 默认为true 打印banner 若您不期望打印banner,可配置为false
global-config:
i-kun-mode: true # 是否开启小黑子模式,默认关闭, 开启后日志将更有趣,提升编码乐趣,仅供娱乐,切勿用于其它任何用途
process-index-mode: manual #索引处理模式,smoothly:平滑模式, not_smoothly:非平滑模式, manual:手动模式,,默认开启此模式
print-dsl: true # 开启控制台打印通过本框架生成的DSL语句,默认为开启,测试稳定后的生产环境建议关闭,以提升少量性能
distributed: false # 当前项目是否分布式项目,默认为true,在非手动托管索引模式下,若为分布式项目则会获取分布式锁,非分布式项目只需synchronized锁.
reindexTimeOutHours: 72 # 重建索引超时时间 单位小时,默认72H 可根据ES中存储的数据量调整
async-process-index-blocking: true # 异步处理索引是否阻塞主线程 默认阻塞 数据量过大时调整为非阻塞异步进行 项目启动更快
active-release-index-max-retry: 4320 # 分布式环境下,平滑模式,当前客户端激活最新索引最大重试次数,若数据量过大,重建索引数据迁移时间超过4320/60=72H,可调大此参数值,此参数值决定最大重试次数,超出此次数后仍未成功,则终止重试并记录异常日志
active-release-index-fixed-delay: 60 # 分布式环境下,平滑模式,当前客户端激活最新索引最大重试次数 分布式环境下,平滑模式,当前客户端激活最新索引重试时间间隔 若您期望最终一致性的时效性更高,可调小此值,但会牺牲一些性能
实体类
@Data
@IndexName(value = "ping")
public class EsShopping {
@IndexId(type= IdType.CUSTOMIZE)
private Integer id;
@IndexField(value = "src")
private String src;
@IndexField(value = "name",
fieldType = FieldType.TEXT,
analyzer = Analyzer.IK_MAX_WORD,
searchAnalyzer = Analyzer.IK_MAX_WORD)
@HighLight(preTag = "<span style=\"color:red\">",postTag = "</span>")
private String name;
@IndexField(fieldType = FieldType.DOUBLE)
private Double jia;
@IndexField(fieldType = FieldType.INTEGER)
private Integer xiao;
@IndexField(fieldType = FieldType.INTEGER)
private Integer ku;
@IndexField(fieldType = FieldType.INTEGER)
private Integer pai;
@IndexField(fieldType = FieldType.INTEGER)
private Integer typeId;
@IndexField(fieldType = FieldType.INTEGER)
private Integer zhuang;
}
mapper
public interface EsShoppingMapper extends BaseEsMapper<EsShopping> {
}
继承BaseEsMapper,原理和mybatilplus一样
注意!!!es的mapper和mybatisplus的mapper不要放在一起
启动类添加注解扫描mapper
@EsMapperScan("")
业务代码
@Resource
private EsShoppingMapper esShoppingMapper;
//删除索引数据
Boolean aBoolean = esShoppingMapper.deleteIndex(INDEX_NAME);
//批量添加
esShoppingMapper.insertBatch(esShoppings);
//修改
esShoppingMapper.updateById(JSON.parseObject(msg, EsShopping.class));
//根据id删除数据
esShoppingMapper.deleteById(id);
//查询数据
esShoppingMapper.selectById(id);
//分页 + 高亮
LambdaEsQueryWrapper<EsShopping> esShoppingLambdaEsQueryWrapper = new LambdaEsQueryWrapper<>();
if (requestShopping.getTypeId() != null){
esShoppingLambdaEsQueryWrapper.eq(EsShopping::getTypeId,requestShopping.getTypeId());
}
//高亮
if (StringUtils.isNoneBlank(requestShopping.getName())){
esShoppingLambdaEsQueryWrapper.match(EsShopping::getName,requestShopping.getName());
}
return esShoppingMapper.pageQuery(esShoppingLambdaEsQueryWrapper,requestShopping.getPageNumber(),requestShopping.getPageSize());
其他功能可以查看BaseEsMapper