原创 spring设计 [spring设计](javascript:void(0)😉 2022-10-09 16:39 发表于云南
文章目录
easy-es介绍
简介
Easy-Es(简称EE)是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的ORM开发框架,在 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生,您如果有用过Mybatis-Plus(简称MP),那么您基本可以零学习成本直接上手EE,EE是MP的Es平替版,在有些方面甚至比MP更简单,同时也融入了更多Es独有的功能,助力您快速实现各种场景的开发.
框架架构
适用场景
-
• 检索类服务
-
- • 搜索文库
- • 电商商品检索
- • 海量系统日志检索
-
• 问答类服务
-
- • 在线智能客服
- • 机器人
-
• 地图类服务
-
- • 打车app
- • 外卖app
- • 社区团购配送
- • 陌生人交友
以上就是我个人能够想到的 当前版本为v1.0.2,es客户端为7.14.0 如果使用版本过低的es客户端,可能会让你当场去世
快速开始
废话不多说 直接上代码
引入依赖
<!--引入easy-es最新版依赖文件--><dependency>
<groupId>cn.easy-es</groupId>
<artifactId>easy-es-boot-starter</artifactId>
<version>1.0.2</version>
</dependency>
创建es实体模型设计(此处以订单检索为例)
package com.cn.reddog.goods.server.estity;
import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.common.constants.Analyzer;
import cn.easyes.common.enums.FieldType;
import cn.easyes.common.enums.IdType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.elasticsearch.index.analysis.Analysis;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
/**
* @author apple
* @desc es 订单数据模型设计
*/
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@IndexName(value = "goods_spu_es",keepGlobalPrefix = true)
public class GoodsSpuEs implements Serializable {
@Serial
private static final long serialVersionUID = 8596716425733775320L;
@IndexId(type = IdType.CUSTOMIZE)
private Long id;
@IndexField(fieldType = FieldType.KEYWORD_TEXT,fieldData = true,analyzer = Analyzer.IK_SMART)
private String keyWord;
@IndexField(fieldType = FieldType.KEYWORD_TEXT,fieldData = true,analyzer = Analyzer.IK_SMART)
private String title;
/**
* 品牌名称
*/
@IndexField(fieldType = FieldType.KEYWORD_TEXT,fieldData = true,analyzer = Analyzer.IK_SMART)
private String brandName;
/**
* 商家名
*/
@IndexField(fieldType = FieldType.KEYWORD_TEXT,fieldData = true,analyzer = Analyzer.IK_SMART)
private String merchantName;
/**
* 类别
*/
@IndexField(fieldType = FieldType.ARRAY,analyzer = Analyzer.IK_SMART)
private String category;
/**
* 标签
*/
@IndexField(fieldType = FieldType.ARRAY,analyzer = Analyzer.IK_SMART)
private String tags;
/**
* 营销类型
*/
@IndexField(fieldType = FieldType.ARRAY,analyzer = Analyzer.IK_SMART)
private String saleType;
/**
* 宝贝id 上述id已替换为商品id
*/
@Deprecated
private Long goodsId;
}
创建esMapper类(GoodsSpuEsMapper)
package com.cn.reddog.goods.server.es;
import cn.easyes.core.conditions.interfaces.BaseEsMapper;
import com.cn.reddog.goods.server.estity.GoodsSpuEs;
/**
* @author apple
* @desc es 商品检索mapper
*/
public interface GoodsSpuEsMapper extends BaseEsMapper<GoodsSpuEs> {
}
service方法实现
package com.cn.reddog.goods.server.service;
import com.cn.reddog.goods.api.entity.GoodsSpu;
import com.cn.reddog.goods.server.estity.GoodsSpuEs;
import org.elasticsearch.action.search.SearchResponse;
import java.util.List;
/**
* @author apple
* @desc 商品检索相关
*/
public interface GoodsSearchService {
/**
* 检索
* @param keyword 关键词
* @return
*/
List<GoodsSpuEs> search(String keyword);
/**
* 新增商品信息
* @param goodsSpus spu信息
*/
Boolean insertGoodsInfo(List<GoodsSpu>goodsSpus);
/**
* 移除与当前商品id相关的所有字段信息
* @param goodsId 商品id
* @return
*/
Boolean removeByGoodsInfo(Long goodsId);
}
请求类
package com.cn.reddog.goods.server.controller;
import com.cn.reddog.core.tool.result.R;
import com.cn.reddog.goods.server.service.GoodsSearchService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author apple
* @desc 商品检索相关
*/
@RestController
@RequestMapping("/sem/wssearch")
@RequiredArgsConstructor
public class GoodsEsController {
private final GoodsSearchService goodsSearchService;
/**
* 商品关键字查询
* @param keyword 关键字
* @return
*/
@GetMapping("/search")
public R<?> goodsSearch(@RequestParam String keyword){
return R.data(goodsSearchService.search(keyword));
}
}
启动类
package com.cn.reddog.goods.server;
import cn.easyes.starter.register.EsMapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringCloudApplication
@ComponentScan(basePackages = "com.cn.reddog")
@EsMapperScan("com.cn.reddog.goods.server.es") //需要特别注意此项,主要为es的mapper扫描包 详情可见es-seay官网
public class GoodsApplication {
public static void main(String[] args) {
SpringApplication.run(GoodsApplication.class,args);
}
}
yml配置
easy-es:
enable: true #默认为true,若为false则认为不启用本框架
address : ${spring.elasticsearch.rest.uris} # es的连接地址,必须含端口 若为集群,则可以用逗号隔开 例如:127.0.0.1:9200,127.0.0.2:9200
schema: http
# username: elastic
# password: WG7WVmuNMtM4GwNYkyWH
keep-alive-millis: 18000
global-config:
# print-dsl: true # 开启控制台打印通过本框架生成的DSL语句,默认为开启,测试稳定后的生产环境建议关闭,以提升少量性能
process-index-mode: smoothly
async-process-index-blocking: true
db-config:
map-underscore-to-camel-case: true
# table-prefix: dev_
id-type: customize
field-strategy: not_empty
refresh-policy: immediate
enable-track-total-hits: true
enable-must2-filter: false
banner: true
logging:
level:
tracer: trace # 开启trace级别日志,在开发时可以开启此配置,则控制台可以打印es全部请求信息及DSL语句,为了避免重复,开启此项配置后,可以将EE的print-dsl设置为false.
最终效果入图
结果:
{
"code": 20000,
"success": true,
"data": [
{
"id": "1577579055978409986",
"keyWord": "华为nova9全新上市",
"title": "华为nova9",
"brandName": "华为",
"merchantName": "测试店铺",
"category": "[{\"code\":\"1500025564343398401\",\"name\":\"手机\"},{\"code\":\"1500029340139745282\",\"name\":\"手机\"},{\"code\":\"2\",\"name\":\"手机通讯\"}]",
"tags": "[\"国庆季\"]",
"saleType": "[\"新品\"]",
"goodsId": "1577579055978409986"
},
{
"id": "1577581638042284033",
"keyWord": "测试商品勿拍全新上市",
"title": "测试商品勿拍",
"brandName": "苹果",
"merchantName": "测试店铺",
"category": "[{\"code\":\"1500025564343398401\",\"name\":\"手机\"},{\"code\":\"1500029340139745282\",\"name\":\"手机\"},{\"code\":\"2\",\"name\":\"手机通讯\"}]",
"tags": "[\"国庆季\",\"618\",\"双12\"]",
"saleType": "[\"新品\"]",
"goodsId": "1577581638042284033"
},
{
"id": "1577579281569050625",
"keyWord": "华为nova9全新上市",
"title": "华为nova9",
"brandName": "华为",
"merchantName": "测试店铺",
"category": "[{\"code\":\"1500025564343398401\",\"name\":\"手机\"},{\"code\":\"1500029340139745282\",\"name\":\"手机\"},{\"code\":\"2\",\"name\":\"手机通讯\"}]",
"tags": "[\"重阳节\",\"国庆季\",\"双11\"]",
"saleType": "[\"新品\",\"热销\"]",
"goodsId": "1577579281569050625"
},
{
"id": "1577579566211297281",
"keyWord": "华为nova9全新上市",
"title": "华为nova9",
"brandName": "华为",
"merchantName": "测试店铺",
"category": "[{\"code\":\"1500025564343398401\",\"name\":\"手机\"},{\"code\":\"1500029340139745282\",\"name\":\"手机\"},{\"code\":\"2\",\"name\":\"手机通讯\"}]",
"tags": "[\"国庆季\",\"元旦季\",\"双11\"]",
"saleType": "[\"热销\"]",
"goodsId": "1577579566211297281"
}
],
"msg": "OK",
"sub_code": "SUCCESS",
"sub_msg": "OK",
"sign": ""
}
总结
无论是什么样的技术什么的难点,都有最佳解决方案,最终目标,让复杂问题简单化,让天下无难点技术! 代码里未涉及到相关类操作,详情查看官网:
easy-es官网地址:https://www.easy-es.cn/pages/12283a/#_2-pom