一、檢索服務
1、檢索業務分析
商品檢索三個入口
(1)選擇分類進入商品檢索
(2)輸入檢索關鍵字展示檢索頁

(3)選擇篩選條件進入

2、搭建頁面環境
請先參考文章 谷粒商城分布式高級(一)—— 環境搭建(高級篇補充)(ElasticSearch & nginx) 中的 “3、搭建域名訪問環境(反向代理配置 & 負載均衡到網關)”
(1)gulimall-search 導入thymeleaf依賴、熱部署依賴devtools使頁面實時生效
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--模板引擎:thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
(2)html\搜索頁\index.htmk 放到gulimall-search下的templates文件夾,並且修改html命名空間 xmlns:th="http://www.thymeleaf.org"
(3)上傳文件到nginx,實現動靜分離
(a)虛擬機 /mydata/nginx/html/static 新建 search 文件夾

(b)將以下靜態資源上傳到 /mydata/nginx/html/static/search 文件夾


(4)配置域名轉發
(a)修改 Windows 的 hosts文件,映射 search.gulimall.com 到 192.168.56.10(虛擬機地址)
打開 SwitchHosts 操作即可

(b)修改nginx配置 /mydata/nginx/conf/conf.d/gulimall.conf


保存重啟nginx容器
(5)修改網關配置 實現 負載均衡到網關
(a)配置gulimall-gateway(網關服務),將域名為**.gulimall.com 的 “**.”去掉,並新增配置:將域名為search.gulimall.com轉發至search服務

(6)重啟網關,啟動gulimall-search、gulimall.gateway
訪問:http://search.gulimall.com

3、調整頁面跳轉
(1)修改配置文件 application.properties
(2)修改實現:點擊分類跳轉搜索頁面
(a)修改nginx中的文件 /mydata/nginx/html/static/index/js/catalogLoader.js

(b)修改gulimall-product的 templates/index.html 名稱為 templates/index.html
(c)新建文件 com.atguigu.gulimall.search.controller.SearchController
package com.atguigu.gulimall.search.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SearchController {
@GetMapping("/list.html")
public String listPage(){
return "list";
}
}
(d)測試點擊分類跳轉搜索頁面

(3)修改實現:輸入文字,點擊搜索框跳轉搜索頁面
(a)修改 gulimall-product 的 templates\index.html


(b)測試效果


4、檢索查詢參數 / 返回結果 模型分析抽取
商城檢索條件分析。然后把前端傳來的所有可能的查詢條件的數據封裝在一個vo中,利用springmvc自動把前端傳來的數據封裝為一個對象進行接收,相當的方便
(1)新建文件 SearchParam 抽取查詢參數
package com.atguigu.gulimall.search.vo;
import lombok.Data;
import java.util.List;
/**
* 封裝頁面所有可能傳遞過來的關鍵字
* keyword=小米&sort=saleCount_desc/asc&hasStock=0/1&skuPrice=400_1900&brandId=1 &catalogId=1&attrs=1_3G:4G:5G&attrs=2_驍龍 845&attrs=4_高清屏
*/
@Data
public class SearchParam {
/**
* 頁面傳遞過來的全文匹配關鍵字
*/
private String keyword;
/**
* 品牌id,可以多選
*/
private List<Long> brandId;
/**
* 三級分類id
*/
private Long catalog3Id;
/**
* 排序條件:sort=price/salecount/hotscore_desc/asc
*/
private String sort;
/**
* 是否顯示有貨
*/
private Integer hasStock;
/**
* 價格區間查詢
*/
private String skuPrice;
/**
* 按照屬性進行篩選
*/
private List<String> attrs;
/**
* 頁碼
*/
private Integer pageNum = 1;
/**
* 原生的所有查詢條件
*/
private String _queryString;
}
(2)新建文件 SearchResult 抽取返回結果
商城檢索返回的數據分析,把查詢到的數據封裝在一個vo中,返回給前端
package com.atguigu.gulimall.search.vo;
import com.atguigu.common.to.es.SkuEsModel;
import lombok.Data;
import java.util.List;
@Data
public class SearchResult {
/**
* 查詢到的所有商品信息
*/
private List<SkuEsModel> product;
/**
* 當前頁碼
*/
private Integer pageNum;
/**
* 總記錄數
*/
private Long total;
/**
* 總頁碼
*/
private Integer totalPages;
private List<Integer> pageNavs;
/**
* 當前查詢到的結果,所有涉及到的品牌
*/
private List<BrandVo> brands;
/**
* 當前查詢到的結果,所有涉及到的所有屬性
*/
private List<AttrVo> attrs;
/**
* 當前查詢到的結果,所有涉及到的所有分類
*/
private List<CatalogVo> catalogs;
//===========================以上是返回給頁面的所有信息============================//
/* 面包屑導航數據 */
private List<NavVo> navs;
@Data
public static class NavVo {
private String navName;
private String navValue;
private String link;
}
@Data
public static class BrandVo {
private Long brandId;
private String brandName;
private String brandImg;
}
@Data
public static class AttrVo {
private Long attrId;
private String attrName;
private List<String> attrValue;
}
@Data
public static class CatalogVo {
private Long catalogId;
private String catalogName;
}
}
(3)修改com.atguigu.gulimall.search.controller.SearchController 的 listPage 方法
package com.atguigu.gulimall.search.controller;
import com.atguigu.gulimall.search.service.MallSearchService;
import com.atguigu.gulimall.search.vo.SearchParam;
import com.atguigu.gulimall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SearchController {
@Autowired
MallSearchService mallSearchService;
/**
* 自動將頁面提交過來的所有請求查詢參數封裝成指定的對象
* @param param
* @return
*/
@GetMapping("/list.html")
public String listPage(SearchParam param, Model model){
//1、根據傳遞過來的頁面的查詢參數,去es中檢索商品
SearchResult result = mallSearchService.search(param);
model.addAttribute("result",result);
return "list";
}
}
(4)新增實現類和方法 search
package com.atguigu.gulimall.search.service.impl;
import com.atguigu.gulimall.search.service.MallSearchService;
import com.atguigu.gulimall.search.vo.SearchParam;
import com.atguigu.gulimall.search.vo.SearchResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class MallSearchServiceImpl implements MallSearchService {
/**
* 檢索
* @param param:檢索的所有參數
* @return
*/
@Override
public SearchResult search(SearchParam param) {
return null;
}
}
5、檢索DSL測試—查詢部分
先在kibanna中用es的DSL測試
(1)search.gmall.com/list.html?keyword=華為
全文匹配用 must 里的 match
GET product/_search { "query": { "bool": { "must": [ { "match": { "skuTitle": "華為" } } ] } } }
(2)search.gmall.com/list.html?keyword=華為&catalogId=225
分類、屬性、價格這些不需要參與評分的寫在filter里。(也可以在must里match后面接着term,但是既然不需要評分,就可以寫在filter中)
GET product/_search { "query": { "bool": { "must": [ { "match": { "skuTitle": "華為" } } ], "filter": [ { "term": { "catalogId":"225" } } ] } } }
(3)search.gmall.com/list.html?keyword=華為&catalogId=225&brandId=1&brandId=2&brandId=9
brandId是個數組,一個屬性、多個值,用terms
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
}
]
}
}
}
(4)search.gmall.com/list.html?keyword=華為&catalogId=225&brandId=1&brandId=2&brandId=9&attrs=1_5.56&attrs=2_白色:藍色
attrs是nested的(嵌入式的),查詢的時候要用嵌入式的查詢語句
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官網信息為准"
]
}
}
]
}
}
}
}
]
}
}
}
(5)search.gmall.com/list.html?keyword=華為&catalogId=225&brandId=1&brandId=2&brandId=9&attrs=1_5.56&attrs=2_白色:藍色&hasStock=true
有無庫存繼續 term
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官網信息為准"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": {
"value": "false"
}
}
}
]
}
}
}
(6)search.gmall.com/list.html?keyword=華為&catalogId=225&brandId=1&brandId=2&brandId=9&attrs=1_5.56&attrs=2_白色:藍色&hasStock=true&skuPrice=_6000 按價格區間檢索用range
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官網信息為准"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": {
"value": "false"
}
}
},
{
"range": {
"skuPrice": {
"gte": 0,
"lte": 6000
}
}
}
]
}
}
}
(7)search.gmall.com/list.html?keyword=華為&catalogId=225&brandId=1&brandId=2&brandId=9&attrs=1_5.56&attrs=2_白色:藍色&hasStock=true&skuPrice=_6000&sort=skuPrice_desc 排序是與查詢並列的,在query后面寫sort
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官網信息為准"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": {
"value": "false"
}
}
},
{
"range": {
"skuPrice": {
"gte": 0,
"lte": 6000
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
]
}
(8)search.gmall.com/list.html?keyword=華為&catalogId=225&brandId=1&brandId=2&brandId=9&attrs=1_5.56&attrs=2_白色:藍色&hasStock=true&skuPrice=_6000&sort=skuPrice_desc&pageNum=4 分頁用from和size,"from":x,"size":y表示從x開始查y個
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官網信息為准"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": {
"value": "false"
}
}
},
{
"range": {
"skuPrice": {
"gte": 0,
"lte": 6000
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
],
"from": 0,
"size": 5
}
(9)高亮全文查詢關鍵詞
GET product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId":"225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官網信息為准"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": {
"value": "false"
}
}
},
{
"range": {
"skuPrice": {
"gte": 0,
"lte": 6000
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"highlight": {
"fields": {
"skuTitle": {}
},
"pre_tags": "<b style='color:red'>",
"post_tags": "</b>"
}
}
6、檢索DSL測試—聚合部分
聚合分析(從所有查詢結果中提取出相關的屬性等信息)
使用agg

我們想聚合分析得到屬性名字和分類名字,應該怎么做呢?可以通過子聚合直接得到,子聚合會用到父聚合的結果再次進行聚合

要想對 brandName 進行聚合要求它的 doc_values 為 true,所以需要對索引進行修改,修改方式:1.新建一個索引 2.數據遷移

新建索引
PUT gmall_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": "gmall_product"
}
}
記得把Java中商品上架當時寫的那個索引名改過來


聚合屬性,與品牌、分類不同,由於屬性是嵌入式的,所以聚合也得用嵌入式的

最終所有的查詢語句
GET gmall_product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "華為"
}
}
],
"filter": [
{
"term": {
"catalogId": "225"
}
},
{
"terms": {
"brandId": [
"2"
]
}
},
{
"term": {
"hasStock": "false"
}
},
{
"range": {
"skuPrice": {
"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": {
"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
}
}
}
}
}
}
}
}
7、SearchRequest構建和SearchResponse分析&封裝
package com.atguigu.gulimall.search.service.impl; import com.alibaba.fastjson.JSON; import com.atguigu.common.to.es.SkuEsModel; import com.atguigu.common.utils.R; import com.atguigu.gulimall.search.config.GulimallElasticSearchConfig; import com.atguigu.gulimall.search.constant.EsConstant; import com.atguigu.gulimall.search.service.MallSearchService; import com.atguigu.gulimall.search.vo.SearchParam; import com.atguigu.gulimall.search.vo.SearchResult; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.nested.ParsedNested; import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms; import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @Slf4j @Service public class MallSearchServiceImpl implements MallSearchService { @Resource private RestHighLevelClient esRestClient; @Override public SearchResult search(SearchParam param) { //1、動態構建出查詢需要的DSL語句 SearchResult result = null; //1、准備檢索請求 SearchRequest searchRequest = buildSearchRequest(param); try { //2、執行檢索請求 SearchResponse response = esRestClient.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS); //3、分析響應數據,封裝成我們需要的格式 result = buildSearchResult(response,param); } catch (IOException e) { e.printStackTrace(); } return result; } /** * 構建結果數據 * 模糊匹配,過濾(按照屬性、分類、品牌,價格區間,庫存),完成排序、分頁、高亮,聚合分析功能 * @param response * @return */ private SearchResult buildSearchResult(SearchResponse response,SearchParam param) { SearchResult result = new SearchResult(); //1、返回的所有查詢到的商品 SearchHits hits = response.getHits(); List<SkuEsModel> esModels = new ArrayList<>(); //遍歷所有商品信息 if (hits.getHits() != null && hits.getHits().length > 0) { for (SearchHit hit : hits.getHits()) { String sourceAsString = hit.getSourceAsString(); SkuEsModel esModel = JSON.parseObject(sourceAsString, SkuEsModel.class); //判斷是否按關鍵字檢索,若是就顯示高亮,否則不顯示 if (!StringUtils.isEmpty(param.getKeyword())) { //拿到高亮信息顯示標題 HighlightField skuTitle = hit.getHighlightFields().get("skuTitle"); String skuTitleValue = skuTitle.getFragments()[0].string(); esModel.setSkuTitle(skuTitleValue); } esModels.add(esModel); } } result.setProduct(esModels); //2、當前商品涉及到的所有屬性信息 List<SearchResult.AttrVo> attrVos = new ArrayList<>(); //獲取屬性信息的聚合 ParsedNested attrsAgg = response.getAggregations().get("attr_agg"); ParsedLongTerms attrIdAgg = attrsAgg.getAggregations().get("attr_id_agg"); for (Terms.Bucket bucket : attrIdAgg.getBuckets()) { SearchResult.AttrVo attrVo = new SearchResult.AttrVo(); //1、得到屬性的id long attrId = bucket.getKeyAsNumber().longValue(); attrVo.setAttrId(attrId); //2、得到屬性的名字 ParsedStringTerms attrNameAgg = bucket.getAggregations().get("attr_name_agg"); String attrName = attrNameAgg.getBuckets().get(0).getKeyAsString(); attrVo.setAttrName(attrName); //3、得到屬性的所有值 ParsedStringTerms attrValueAgg = bucket.getAggregations().get("attr_value_agg"); List<String> attrValues = attrValueAgg.getBuckets().stream().map(item -> item.getKeyAsString()).collect(Collectors.toList()); attrVo.setAttrValue(attrValues); attrVos.add(attrVo); } result.setAttrs(attrVos); //3、當前商品涉及到的所有品牌信息 List<SearchResult.BrandVo> brandVos = new ArrayList<>(); //獲取到品牌的聚合 ParsedLongTerms brandAgg = response.getAggregations().get("brand_agg"); for (Terms.Bucket bucket : brandAgg.getBuckets()) { SearchResult.BrandVo brandVo = new SearchResult.BrandVo(); //1、得到品牌的id long brandId = bucket.getKeyAsNumber().longValue(); brandVo.setBrandId(brandId); //2、得到品牌的名字 ParsedStringTerms brandNameAgg = bucket.getAggregations().get("brand_name_agg"); String brandName = brandNameAgg.getBuckets().get(0).getKeyAsString(); brandVo.setBrandName(brandName); //3、得到品牌的圖片 ParsedStringTerms brandImgAgg = bucket.getAggregations().get("brand_img_agg"); String brandImg = brandImgAgg.getBuckets().get(0).getKeyAsString(); brandVo.setBrandImg(brandImg); brandVos.add(brandVo); } result.setBrands(brandVos); //4、當前商品涉及到的所有分類信息 //獲取到分類的聚合 List<SearchResult.CatalogVo> catalogVos = new ArrayList<>(); ParsedLongTerms catalogAgg = response.getAggregations().get("catalog_agg"); for (Terms.Bucket bucket : catalogAgg.getBuckets()) { SearchResult.CatalogVo catalogVo = new SearchResult.CatalogVo(); //得到分類id String keyAsString = bucket.getKeyAsString(); catalogVo.setCatalogId(Long.parseLong(keyAsString)); //得到分類名 ParsedStringTerms catalogNameAgg = bucket.getAggregations().get("catalog_name_agg"); String catalogName = catalogNameAgg.getBuckets().get(0).getKeyAsString(); catalogVo.setCatalogName(catalogName); catalogVos.add(catalogVo); } result.setCatalogs(catalogVos); //===============以上可以從聚合信息中獲取====================// //5、分頁信息-頁碼 result.setPageNum(param.getPageNum()); //5、1分頁信息、總記錄數 long total = hits.getTotalHits().value; result.setTotal(total); //5、2分頁信息-總頁碼-計算 int totalPages = (int)total % EsConstant.PRODUCT_PAGESIZE == 0 ? (int)total / EsConstant.PRODUCT_PAGESIZE : ((int)total / EsConstant.PRODUCT_PAGESIZE + 1); result.setTotalPages(totalPages); List<Integer> pageNavs = new ArrayList<>(); for (int i = 1; i <= totalPages; i++) { pageNavs.add(i); } result.setPageNavs(pageNavs); return result; } /** * 准備檢索請求 * 模糊匹配,過濾(按照屬性,分類,品牌,價格區間,庫存),排序,分頁,高亮,聚合分析 * @return */ private SearchRequest buildSearchRequest(SearchParam param) { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); /** * 模糊匹配,過濾(按照屬性,分類,品牌,價格區間,庫存) */ //1. 構建bool-query BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder(); //1.1 bool-must if(!StringUtils.isEmpty(param.getKeyword())){ boolQueryBuilder.must(QueryBuilders.matchQuery("skuTitle",param.getKeyword())); } //1.2 bool-fiter //1.2.1 catelogId if(null != param.getCatalog3Id()){ boolQueryBuilder.filter(QueryBuilders.termQuery("catalogId",param.getCatalog3Id())); } //1.2.2 brandId if(null != param.getBrandId() && param.getBrandId().size() >0){ boolQueryBuilder.filter(QueryBuilders.termsQuery("brandId",param.getBrandId())); } //1.2.3 attrs if(param.getAttrs() != null && param.getAttrs().size() > 0){ param.getAttrs().forEach(item -> { //attrs=1_5寸:8寸&2_16G:8G BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); //attrs=1_5寸:8寸 String[] s = item.split("_"); String attrId=s[0]; String[] attrValues = s[1].split(":");//這個屬性檢索用的值 boolQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId)); boolQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues)); NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("attrs",boolQuery, ScoreMode.None); boolQueryBuilder.filter(nestedQueryBuilder); }); } //1.2.4 hasStock if(null != param.getHasStock()){ boolQueryBuilder.filter(QueryBuilders.termQuery("hasStock",param.getHasStock() == 1)); } //1.2.5 skuPrice if(!StringUtils.isEmpty(param.getSkuPrice())){ //skuPrice形式為:1_500或_500或500_ RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("skuPrice"); String[] price = param.getSkuPrice().split("_"); if(price.length==2){ rangeQueryBuilder.gte(price[0]).lte(price[1]); }else if(price.length == 1){ if(param.getSkuPrice().startsWith("_")){ rangeQueryBuilder.lte(price[1]); } if(param.getSkuPrice().endsWith("_")){ rangeQueryBuilder.gte(price[0]); } } boolQueryBuilder.filter(rangeQueryBuilder); } //封裝所有的查詢條件 searchSourceBuilder.query(boolQueryBuilder); /** * 排序,分頁,高亮 */ //排序 //形式為sort=hotScore_asc/desc if(!StringUtils.isEmpty(param.getSort())){ String sort = param.getSort(); String[] sortFileds = sort.split("_"); SortOrder sortOrder="asc".equalsIgnoreCase(sortFileds[1])?SortOrder.ASC:SortOrder.DESC; searchSourceBuilder.sort(sortFileds[0],sortOrder); } //分頁 searchSourceBuilder.from((param.getPageNum()-1)*EsConstant.PRODUCT_PAGESIZE); searchSourceBuilder.size(EsConstant.PRODUCT_PAGESIZE); //高亮 if(!StringUtils.isEmpty(param.getKeyword())){ HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.field("skuTitle"); highlightBuilder.preTags("<b style='color:red'>"); highlightBuilder.postTags("</b>"); searchSourceBuilder.highlighter(highlightBuilder); } /** * 聚合分析 */ //1. 按照品牌進行聚合 TermsAggregationBuilder brand_agg = AggregationBuilders.terms("brand_agg"); brand_agg.field("brandId").size(50); //1.1 品牌的子聚合-品牌名聚合 brand_agg.subAggregation(AggregationBuilders.terms("brand_name_agg") .field("brandName").size(1)); //1.2 品牌的子聚合-品牌圖片聚合 brand_agg.subAggregation(AggregationBuilders.terms("brand_img_agg") .field("brandImg").size(1)); searchSourceBuilder.aggregation(brand_agg); //2. 按照分類信息進行聚合 TermsAggregationBuilder catalog_agg = AggregationBuilders.terms("catalog_agg"); catalog_agg.field("catalogId").size(20); catalog_agg.subAggregation(AggregationBuilders.terms("catalog_name_agg").field("catalogName").size(1)); searchSourceBuilder.aggregation(catalog_agg); //2. 按照屬性信息進行聚合 NestedAggregationBuilder attr_agg = AggregationBuilders.nested("attr_agg", "attrs"); //2.1 按照屬性ID進行聚合 TermsAggregationBuilder attr_id_agg = AggregationBuilders.terms("attr_id_agg").field("attrs.attrId"); attr_agg.subAggregation(attr_id_agg); //2.1.1 在每個屬性ID下,按照屬性名進行聚合 attr_id_agg.subAggregation(AggregationBuilders.terms("attr_name_agg").field("attrs.attrName").size(1)); //2.1.1 在每個屬性ID下,按照屬性值進行聚合 attr_id_agg.subAggregation(AggregationBuilders.terms("attr_value_agg").field("attrs.attrValue").size(50)); searchSourceBuilder.aggregation(attr_agg); log.debug("構建的DSL語句 {}",searchSourceBuilder.toString()); SearchRequest searchRequest = new SearchRequest(new String[]{EsConstant.PRODUCT_INDEX},searchSourceBuilder); return searchRequest; } }
8、頁面基本數據渲染
修改默認分頁數據為16個
修改gulimall-search的templates/list.html,渲染搜索頁面
(1)顯示商品
訪問:http://search.gulimall.com/list.html?catalog3Id=225

訪問:http://search.gulimall.com/list.html?catalog3Id=225&keyword=華為

(2)顯示品牌、分類和篩選條件



效果:

9、頁面篩選條件渲染
(1)品牌篩選

篩選點擊品牌

(2)分類篩選

再篩選點擊分類效果

(3)屬性篩選

再篩選點擊屬性效果

10、頁面分頁數據渲染
(1)搜索效果渲染

效果

(2)分頁效果渲染


效果:

11、頁面排序功能
(1)修改顯示內容和回顯內容

(2)修改原先的 replaceParamVal 為 replaceAndAddParamVal

(3)點擊切換樣式並且跳轉指定位置


最終效果:

12、頁面價格區間搜索
(1)處理搜索 searchProduct 中 keyword 多次拼接問題
效果:多次點擊搜索,不會多次拼接

(2)根據價格區間搜索商品


效果:

(3)僅顯示有貨


效果:

13、面包屑導航
(1)引入springcloud依賴和openfeign遠程依賴
(2)開啟遠程調用

(3)新增遠程調用接口 com.atguigu.gulimall.search.feign.ProductFeignService
package com.atguigu.gulimall.search.feign;
import com.atguigu.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("gulimall-product")
public interface ProductFeignService {
@RequestMapping("/product/attr/info/{attrId}")
public R attrInfo(@PathVariable("attrId") Long attrId);
}
(4)新增文件 com.atguigu.gulimall.search.vo.AttrResponseVo
package com.atguigu.gulimall.search.vo;
import lombok.Data;
@Data
public class AttrResponseVo {
/**
* 屬性id
*/
private Long attrId;
/**
* 屬性名
*/
private String attrName;
/**
* 是否需要檢索[0-不需要,1-需要]
*/
private Integer searchType;
/**
* 屬性圖標
*/
private String icon;
/**
* 可選值列表[用逗號分隔]
*/
private String valueSelect;
/**
* 屬性類型[0-銷售屬性,1-基本屬性,2-既是銷售屬性又是基本屬性]
*/
private Integer attrType;
/**
* 啟用狀態[0 - 禁用,1 - 啟用]
*/
private Long enable;
/**
* 所屬分類
*/
private Long catelogId;
/**
* 快速展示【是否展示在介紹上;0-否 1-是】,在sku中仍然可以調整
*/
private Integer showDesc;
private Long attrGroupId;
private String catelogName;
private String groupName;
private Long[] catelogPath;
}
(5)修改文件 com.atguigu.gulimall.search.controller.SearchController 的 listPage 方法
/**
* 自動將頁面提交過來的所有請求查詢參數封裝成指定的對象
* @param param
* @return
*/
@GetMapping("/list.html")
public String listPage(SearchParam param, Model model, HttpServletRequest request){
String queryString = request.getQueryString();
param.set_queryString(queryString);
//1、根據傳遞過來的頁面的查詢參數,去es中檢索商品
SearchResult result = mallSearchService.search(param);
model.addAttribute("result",result);
return "list";
}
(6)修改 com.atguigu.gulimall.search.service.impl.MallSearchServiceImpl 的 buildSearchResult方法
/**
* 構建結果數據
* 模糊匹配,過濾(按照屬性、分類、品牌,價格區間,庫存),完成排序、分頁、高亮,聚合分析功能
* @param response
* @return
*/
private SearchResult buildSearchResult(SearchResponse response,SearchParam param) {
SearchResult result = new SearchResult();
//1、返回的所有查詢到的商品
SearchHits hits = response.getHits();
List<SkuEsModel> esModels = new ArrayList<>();
//遍歷所有商品信息
if (hits.getHits() != null && hits.getHits().length > 0) {
for (SearchHit hit : hits.getHits()) {
String sourceAsString = hit.getSourceAsString();
SkuEsModel esModel = JSON.parseObject(sourceAsString, SkuEsModel.class);
//判斷是否按關鍵字檢索,若是就顯示高亮,否則不顯示
if (!StringUtils.isEmpty(param.getKeyword())) {
//拿到高亮信息顯示標題
HighlightField skuTitle = hit.getHighlightFields().get("skuTitle");
String skuTitleValue = skuTitle.getFragments()[0].string();
esModel.setSkuTitle(skuTitleValue);
}
esModels.add(esModel);
}
}
result.setProduct(esModels);
//2、當前商品涉及到的所有屬性信息
List<SearchResult.AttrVo> attrVos = new ArrayList<>();
//獲取屬性信息的聚合
ParsedNested attrsAgg = response.getAggregations().get("attr_agg");
ParsedLongTerms attrIdAgg = attrsAgg.getAggregations().get("attr_id_agg");
for (Terms.Bucket bucket : attrIdAgg.getBuckets()) {
SearchResult.AttrVo attrVo = new SearchResult.AttrVo();
//1、得到屬性的id
long attrId = bucket.getKeyAsNumber().longValue();
attrVo.setAttrId(attrId);
//2、得到屬性的名字
ParsedStringTerms attrNameAgg = bucket.getAggregations().get("attr_name_agg");
String attrName = attrNameAgg.getBuckets().get(0).getKeyAsString();
attrVo.setAttrName(attrName);
//3、得到屬性的所有值
ParsedStringTerms attrValueAgg = bucket.getAggregations().get("attr_value_agg");
List<String> attrValues = attrValueAgg.getBuckets().stream().map(item -> item.getKeyAsString()).collect(Collectors.toList());
attrVo.setAttrValue(attrValues);
attrVos.add(attrVo);
}
result.setAttrs(attrVos);
//3、當前商品涉及到的所有品牌信息
List<SearchResult.BrandVo> brandVos = new ArrayList<>();
//獲取到品牌的聚合
ParsedLongTerms brandAgg = response.getAggregations().get("brand_agg");
for (Terms.Bucket bucket : brandAgg.getBuckets()) {
SearchResult.BrandVo brandVo = new SearchResult.BrandVo();
//1、得到品牌的id
long brandId = bucket.getKeyAsNumber().longValue();
brandVo.setBrandId(brandId);
//2、得到品牌的名字
ParsedStringTerms brandNameAgg = bucket.getAggregations().get("brand_name_agg");
String brandName = brandNameAgg.getBuckets().get(0).getKeyAsString();
brandVo.setBrandName(brandName);
//3、得到品牌的圖片
ParsedStringTerms brandImgAgg = bucket.getAggregations().get("brand_img_agg");
String brandImg = brandImgAgg.getBuckets().get(0).getKeyAsString();
brandVo.setBrandImg(brandImg);
brandVos.add(brandVo);
}
result.setBrands(brandVos);
//4、當前商品涉及到的所有分類信息
//獲取到分類的聚合
List<SearchResult.CatalogVo> catalogVos = new ArrayList<>();
ParsedLongTerms catalogAgg = response.getAggregations().get("catalog_agg");
for (Terms.Bucket bucket : catalogAgg.getBuckets()) {
SearchResult.CatalogVo catalogVo = new SearchResult.CatalogVo();
//得到分類id
String keyAsString = bucket.getKeyAsString();
catalogVo.setCatalogId(Long.parseLong(keyAsString));
//得到分類名
ParsedStringTerms catalogNameAgg = bucket.getAggregations().get("catalog_name_agg");
String catalogName = catalogNameAgg.getBuckets().get(0).getKeyAsString();
catalogVo.setCatalogName(catalogName);
catalogVos.add(catalogVo);
}
result.setCatalogs(catalogVos);
//===============以上可以從聚合信息中獲取====================//
//5、分頁信息-頁碼
result.setPageNum(param.getPageNum());
//5、1分頁信息、總記錄數
long total = hits.getTotalHits().value;
result.setTotal(total);
//5、2分頁信息-總頁碼-計算
int totalPages = (int)total % EsConstant.PRODUCT_PAGESIZE == 0 ?
(int)total / EsConstant.PRODUCT_PAGESIZE : ((int)total / EsConstant.PRODUCT_PAGESIZE + 1);
result.setTotalPages(totalPages);
List<Integer> pageNavs = new ArrayList<>();
for (int i = 1; i <= totalPages; i++) {
pageNavs.add(i);
}
result.setPageNavs(pageNavs);
//6、構建面包屑導航
if (param.getAttrs() != null && param.getAttrs().size() > 0) {
List<SearchResult.NavVo> collect = param.getAttrs().stream().map(attr -> {
//1、分析每一個attrs傳過來的參數值
SearchResult.NavVo navVo = new SearchResult.NavVo();
String[] s = attr.split("_");
navVo.setNavValue(s[1]);
R r = productFeignService.attrInfo(Long.parseLong(s[0]));
if (r.getCode() == 0) {
AttrResponseVo data = r.getData("attr", new TypeReference<AttrResponseVo>() {
});
navVo.setNavName(data.getAttrName());
} else {
navVo.setNavName(s[0]);
}
//2、取消了這個面包屑以后,我們要跳轉到哪個地方,將請求的地址url里面的當前置空
//拿到所有的查詢條件,去掉當前
String encode = null;
try {
encode = URLEncoder.encode(attr,"UTF-8");
encode.replace("+","%20"); //瀏覽器對空格的編碼和Java不一樣,差異化處理
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String replace = param.get_queryString().replace("&attrs=" + attr, "");
navVo.setLink("http://search.gulimall.com/list.html?" + replace);
return navVo;
}).collect(Collectors.toList());
result.setNavs(collect);
}
return result;
}
(7)修改 templates/list.html 文件


最終效果:





