Search模块
1.整理前端内容
导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
html放入template目录下,静态资源放到nginx/search目录下
配置域名转发
想实现的效果是查询search.gulimall能访问到检索页面,在nginx中配置,不管是gulimall.com还是前面有前缀的都应该交给网关处理
网关配置:
配置热部署依赖并且配置缓存为false
<!-- 热部署依赖,使页面实时生效--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
spring:
thymeleaf:
cache: false
以后ctrl+f9就可以重新构建了
2.后台的实现(前端内容不介绍了)
实现点击三级分类跳转到list页面--检索页面
@Controller public class SearchController { //转交给检索页面 @GetMapping("/list.html") public String listPage(){ return "list"; } }
之前映射的有错误,现在在es处迁移映射
PUT gulimall_product { "mappings":{ "properties": { "skuId":{ "type": "long" }, "spuId":{ "type": "keyword" }, "skuTitle": { "type": "text", "analyzer": "ik_smart" }, "skuPrice": { "type": "keyword" }, "skuImg" : { "type": "keyword" }, "saleCount":{ "type":"long" }, "hasStock": { "type": "boolean" }, "hotScore": { "type": "long" }, "brandId": { "type": "long" }, "catalogId": { "type": "long" }, "brandName": {"type": "keyword"}, "brandImg":{ "type": "keyword" }, "catalogName": {"type": "keyword" }, "attrs": { "type": "nested", "properties": { "attrId": {"type": "long" }, "attrName": { "type": "keyword" }, "attrValue": {"type": "keyword" } } } } } }
POST _reindex { "source": { "index": "product" }, "dest": { "index": "gulimall_product" } }
构建的查询条件
GET gulimall_product/_search { "query": { "bool": { "must": [ {"match": { "skuTitle": "华为" }} ], # 检索出华为 "filter": [ # 过滤 { "term": { "catalogId": "225" } }, { "terms": {"brandId": [ "2"] } }, { "term": { "hasStock": "false"} }, { "range": { "skuPrice": { # 价格1K~7K "gte": 1000, "lte": 7000 } } }, { "nested": { "path": "attrs", # 聚合名字 "query": { "bool": { "must": [ { "term": { "attrs.attrId": { "value": "6"} } } ] } } } } ] } }, "sort": [ {"skuPrice": {"order": "desc" } } ], "from": 0, "size": 5, "highlight": { "fields": {"skuTitle": {}}, # 高亮的字段 "pre_tags": "<b style='color:red'>", # 前缀 "post_tags": "</b>" }, "aggs": { # 查完后聚合 "brandAgg": { "terms": { "field": "brandId", "size": 10 }, "aggs": { # 子聚合 "brandNameAgg": { # 每个商品id的品牌 "terms": { "field": "brandName", "size": 10 } }, "brandImgAgg": { "terms": { "field": "brandImg", "size": 10 } } } }, "catalogAgg":{ "terms": { "field": "catalogId", "size": 10 }, "aggs": { "catalogNameAgg": { "terms": { "field": "catalogName", "size": 10 } } } }, "attrs":{ "nested": {"path": "attrs" }, "aggs": { "attrIdAgg": { "terms": { "field": "attrs.attrId", "size": 10 }, "aggs": { "attrNameAgg": { "terms": { "field": "attrs.attrName", "size": 10 } } } } } } } }