商城项目(五)整合Elasticsearch实现商品搜索
商城项目(五)整合Elasticsearch实现商品搜索
环境配置
Elasticsearch
Elasticsearch 是一个分布式、可扩展、实时的搜索与数据分析引擎。 它能从项目一开始就赋予你的数据以搜索、分析和探索的能力,可用于实现全文搜索和实时数据统计。
Elasticsearch的安装和使用
下载Elasticsearch6.2.2的zip包,并解压到指定目录,下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-6-2-2
安装中文分词插件,在elasticsearch-6.2.2\bin目录下执行以下命令:elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip
运行bin目录下的elasticsearch.bat启动Elasticsearch
下载Kibana,作为访问Elasticsearch的客户端,请下载6.2.2版本的zip包,并解压到指定目录,下载地址:https://artifacts.elastic.co/downloads/kibana/kibana-6.2.2-windows-x86_64.zip
运行bin目录下的kibana.bat,启动Kibana的用户界面
访问http://localhost:5601 即可打开Kibana的用户界面
Spring Data Elasticsearch
Spring Data Elasticsearch是Spring提供的一种以Spring Data风格来操作数据存储的方式,它可以避免编写大量的样板代码。
常用注解
@Document
//标示映射到Elasticsearch文档上的领域对象
public @interface Document {//索引库名次,mysql中数据库的概念String indexName();//文档类型,mysql中表的概念String type() default "";//默认分片数short shards() default 5;//默认副本数量short replicas() default 1;}
@Id
//表示是文档的id,文档可以认为是mysql中表行的概念
public @interface Id {}
@Field
public @interface Field {//文档中字段的类型FieldType type() default FieldType.Auto;//是否建立倒排索引boolean index() default true;//是否进行存储boolean store() default false;//分词器名次String analyzer() default "";
}
//为文档自动指定元数据类型
public enum FieldType {Text,//会进行分词并建了索引的字符类型Integer,Long,Date,Float,Double,Boolean,Object,Auto,//自动判断字段类型Nested,//嵌套对象类型Ip,Attachment,Keyword//不会进行分词建立索引的类型
}
Sping Data方式的数据操作
继承ElasticsearchRepository接口可以获得常用的数据操作方法
可以使用衍生查询
在接口中直接指定查询方法名称便可查询,无需进行实现,如商品表中有商品名称、标题和关键字,直接定义以下查询,就可以对这三个字段进行全文搜索。
/*** 搜索查询** @param name 商品名称* @param subTitle 商品标题* @param keywords 商品关键字* @param page 分页信息* @return*/Page<EsProduct> findByNameOrSubTitleOrKeywords(String name, String subTitle, String keywords, Pageable page);
在idea中直接会提示对应字段
使用@Query注解可以用Elasticsearch的DSL语句进行查询
@Query("{"bool" : {"must" : {"field" : {"name" : "?0"}}}}")
Page<EsProduct> findByName(String name,Pageable pageable);
项目使用表说明
- pms_product:商品信息表
- pms_product_attribute:商品属性参数表
- pms_product_attribute_value:存储产品参数值的表
整合Elasticsearch实现商品搜索
在pom.xml中添加相关依赖
<!--Elasticsearch相关依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch<artifactId>
</dependency>
修改SpringBoot配置文件
修改application.yml文件,在spring节点下添加Elasticsearch相关配置。
data:elasticsearch:repositories:enabled: truecluster-nodes: 127.0.0.1:9300 # es的连接地址及端口号cluster-name: elasticsearch # es集群的名称
添加商品文档对象EsProduct
不需要中文分词的字段设置成@Field(type = FieldType.Keyword)类型,需要中文分词的设置成@Field(analyzer = “ik_max_word”,type = FieldType.Text)类型。
/*** 搜索中的商品信息*/
@Document(indexName = "pms", type = "product",shards = 1,replicas = 0)
public class EsProduct implements Serializable {private static final long serialVersionUID = -1L;@Idprivate Long id;@Field(type = FieldType.Keyword)private String productSn;private Long brandId;@Field(type = FieldType.Keyword)private String brandName;private Long productCategoryId;@Field(type = FieldType.Keyword)private String productCategoryName;private String pic;@Field(analyzer = "ik_max_word",type = FieldType.Text)private String name;@Field(analyzer = "ik_max_word",type = FieldType.Text)private String subTitle;@Field(analyzer = "ik_max_word",type = FieldType.Text)private String keywords;private BigDecimal price;private Integer sale;private Integer newStatus;private Integer recommandStatus;private Integer stock;private Integer promotionType;private Integer sort;@Field(type =FieldType.Nested)private List<EsProductAttributeValue> attrValueList;//省略了所有getter和setter方法
}
添加EsProductRepository接口用于操作Elasticsearch
继承ElasticsearchRepository接口,这样就拥有了一些基本的Elasticsearch数据操作方法,同时定义了一个衍生查询方法。
/*** 商品ES操作类*/
public interface EsProductRepository extends ElasticsearchRepository<EsProduct, Long> {/*** 搜索查询** @param name 商品名称* @param subTitle 商品标题* @param keywords 商品关键字* @param page 分页信息* @return*/Page<EsProduct> findByNameOrSubTitleOrKeywords(String name, String subTitle, String keywords, Pageable page);}
添加EsProductService接口
/*** 商品搜索管理Service*/
public interface EsProductService {/*** 从数据库中导入所有商品到ES*/int importAll();/*** 根据id删除商品*/void delete(Long id);/*** 根据id创建商品*/EsProduct create(Long id);/*** 批量删除商品*/void delete(List<Long> ids);/*** 根据关键字搜索名称或者副标题*/Page<EsProduct> search(String keyword, Integer pageNum, Integer pageSize);}
添加EsProductService接口的实现类EsProductServiceImpl
/*** 商品搜索管理Service实现类*/
@Service
public class EsProductServiceImpl implements EsProductService {private static final Logger LOGGER = LoggerFactory.getLogger(EsProductServiceImpl.class);@Autowiredprivate EsProductDao productDao;@Autowiredprivate EsProductRepository productRepository;@Overridepublic int importAll() {List<EsProduct> esProductList = productDao.getAllEsProductList(null);Iterable<EsProduct> esProductIterable = productRepository.saveAll(esProductList);Iterator<EsProduct> iterator = esProductIterable.iterator();int result = 0;while (iterator.hasNext()) {result++;iterator.next();}return result;}@Overridepublic void delete(Long id) {productRepository.deleteById(id);}@Overridepublic EsProduct create(Long id) {EsProduct result = null;List<EsProduct> esProductList = productDao.getAllEsProductList(id);if (esProductList.size() > 0) {EsProduct esProduct = esProductList.get(0);result = productRepository.save(esProduct);}return result;}@Overridepublic void delete(List<Long> ids) {if (!CollectionUtils.isEmpty(ids)) {List<EsProduct> esProductList = new ArrayList<>();for (Long id : ids) {EsProduct esProduct = new EsProduct();esProduct.setId(id);esProductList.add(esProduct);}productRepository.deleteAll(esProductList);}}@Overridepublic Page<EsProduct> search(String keyword, Integer pageNum, Integer pageSize) {Pageable pageable = PageRequest.of(pageNum, pageSize);return productRepository.findByNameOrSubTitleOrKeywords(keyword, keyword, keyword, pageable);}
}
添加EsProductController定义接口
/*** 搜索商品管理Controller*/
@Controller
@Api(tags = "EsProductController", description = "搜索商品管理")
@RequestMapping("/esProduct")
public class EsProductController {@Autowiredprivate EsProductService esProductService;@ApiOperation(value = "导入所有数据库中商品到ES")@RequestMapping(value = "/importAll", method = RequestMethod.POST)@ResponseBodypublic CommonResult<Integer> importAllList() {int count = esProductService.importAll();return CommonResult.success(count);}@ApiOperation(value = "根据id删除商品")@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)@ResponseBodypublic CommonResult<Object> delete(@PathVariable Long id) {esProductService.delete(id);return CommonResult.success(null);}@ApiOperation(value = "根据id批量删除商品")@RequestMapping(value = "/delete/batch", method = RequestMethod.POST)@ResponseBodypublic CommonResult<Object> delete(@RequestParam("ids") List<Long> ids) {esProductService.delete(ids);return CommonResult.success(null);}@ApiOperation(value = "根据id创建商品")@RequestMapping(value = "/create/{id}", method = RequestMethod.POST)@ResponseBodypublic CommonResult<EsProduct> create(@PathVariable Long id) {EsProduct esProduct = esProductService.create(id);if (esProduct != null) {return CommonResult.success(esProduct);} else {return CommonResult.failed();}}@ApiOperation(value = "简单搜索")@RequestMapping(value = "/search/simple", method = RequestMethod.GET)@ResponseBodypublic CommonResult<CommonPage<EsProduct>> search(@RequestParam(required = false) String keyword,@RequestParam(required = false, defaultValue = "0") Integer pageNum,@RequestParam(required = false, defaultValue = "5") Integer pageSize) {Page<EsProduct> esProductPage = esProductService.search(keyword, pageNum, pageSize);return CommonResult.success(CommonPage.restPage(esProductPage));}
}
进行接口测试
将数据库中数据导入到Elasticsearch
进行商品搜索
商城项目(五)整合Elasticsearch实现商品搜索相关推荐
- 畅购商城(五):Elasticsearch实现商品搜索
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 畅购商城(一):环境搭建 畅购商 ...
- 整合Elasticsearch实现商品搜索
整合Elasticsearch实现商品搜索 本文主要讲解mall整合Elasticsearch的过程,以实现商品信息在Elasticsearch中的导入.查询.修改.删除为例. 项目使用框架介绍 El ...
- ik分词器实现原理_SpringBoot整合Elasticsearch实现商品搜索
本文主要介绍在Elasticsearch中实现商品搜索功能 中文分词器 Elasticsearch有默认的分词器,默认分词器只是将中文逐词分隔,并不符合我们的需求. get hanzo/_analyz ...
- 谷粒商城--整合Elasticsearch和商品的上架
整合Elasticsearch和商品的上架 一.整合ES ES常用概念 索引,类型,文档是什么? 倒排索引 相关度分数score的计算 安装ES和Kibana 快速安装 ES kibana 初步检索_ ...
- Elasticsearch实现商品搜索(关键字查询 条件筛选 规格过滤 价格区间搜索 分页查询 排序查询 高亮查询)
Elasticsearch实现商品搜索 商品搜索 1.根据关键字查询 2.条件筛选 2.1 品牌筛选 2.1.1 需求分析 2.1.2 代码实现 2.2 规格过滤 2.2.1 需求分析 2.2.2 代 ...
- ElasticSearch实现商品搜索与聚合分析
ElasticSearch实现商品搜索与聚合分析 Gitee地址:https://gitee.com/yuyuuyuy/micro-mall 文章目录 ElasticSearch实现商品搜索与聚合分析 ...
- Elasticsearch构建商品搜索系统
搜索这个特性可以说是无处不在,现在很少有网站或者系统不提供搜索功能了,所以,即使你不是一个专业做搜索的程序员,也难免会遇到一些搜索相关的需求.搜索这个东西,表面上看功能很简单,就是一个搜索框,输入关键 ...
- 淘淘商城第56讲——测试一下商品搜索功能
到这里,我相信大家也是不容易,我自己也算是很不容易地写到这里了,希望自己能一直写下去.之前我们就差不多把商品搜索功能实现了,本文我们来一起测试下该商品搜索功能. 首先我们要确保Zookeeper服务器 ...
- JavaWeb商城项目笔记--- Day1 (热门商品,热销商品)
功能出现场景 在线的商场中,前端最近界面总会有一块区域用来显示销售量最高的,最新上架的和类似的这种的商品. 一些联想到的功能:热销,热评等 功能解决思路 核心还是对数据库进行查询,然后响应给前端信息, ...
最新文章
- centos格式化优盘命令_centos 磁盘分区、格式化及挂载
- HDU1013 POJ1519 Digital Roots(解法二)
- 《C++ Primer 4th》读书笔记 第6章-语句
- Struts2_中文问题
- Spring Boot入门(11)实现文件下载功能
- Styled Label
- GitLab怎样实现新建仓库并允许开发者推送代码实现协同开发
- python下的所有文件_python批量复制文件夹下所有文件大小
- OpenCV常见的优化方法和技巧总结
- MATLAB字符串转换函数
- luogu P2516 [HAOI2010]最长公共子序列
- vuex commit 模块_Vuex详细介绍
- SQL--查询无记录,显示默认一条记录
- swift 抛出错误_Swift错误处理– Swift尝试,捕捉,抛出
- 洛谷P2024 食物链
- 热点综述 | 空间组学技术如何全面解码肿瘤微环境
- 51单片机控制双步进电机的魔法师思想
- 【个人博客】Hexo个人博客搭建与配置详细教程 + Fluid主题 + Gitee发布
- element 按钮样式 点击后发生改变(完整改变element按钮样式)
- Node 插件 爬取王者荣耀官网英雄信息,并生成数据库