檢索服務
0、準備環境
achangmall-search
- 引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
- 配置
spring:
thymeleaf:
cache: false
- 放入頁面
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-y7dBYTq2-1633703383247)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008204133612.png)]](https://img.uj5u.com/2021/10/10/272231100752473.png)
- 在index.html整合thymeleaf
<html lang="en" xmlns:th="http:/www.thymeleaf.org">
- 將靜態資源放入nginx中,創建search檔案夾
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-1XCv7osl-1633703383250)(C:\Users\PePe\AppData\Roaming\Typora\typora-user-images\image-20211008205324104.png)]](https://img.uj5u.com/2021/10/10/272231100752474.png)
- 在index.html修改路徑加上
/static/search/
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-saqJAXDJ-1633703383252)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008204650209.png)]](https://img.uj5u.com/2021/10/10/272231100752475.png)
- 配置window轉發:C:\Windows\System32\drivers\etc\hosts
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-iJfnULk2-1633703383262)(C:\Users\PePe\AppData\Roaming\Typora\typora-user-images\image-20211008205807302.png)]](https://img.uj5u.com/2021/10/10/272231100752471.png)
- 阿昌專案啟動報錯:redisson io.netty.channel.epoll.EpollEventLoopGroup
解決方案:移除之前redisson組態檔中的
config.setTransportMode(TransportMode.EPOLL);
- 配置nginx轉發這search服務
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-dtxFaqud-1633703383265)(C:\Users\PePe\AppData\Roaming\Typora\typora-user-images\image-20211008211628201.png)]](https://img.uj5u.com/2021/10/10/272231100752472.png)
- 配置gateway,轉發到search服務
- id: nginx_route
uri: lb://achangmall-product
predicates:
- Host=achangmall.com
- id: search_route
uri: lb://achangmall-search
predicates:
- Host=search.achangmall.com
- 整體的流程
請求search.achangmall.com被nginx轉發給gateway轉發給search服務
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-4IrPDoWQ-1633703383267)(C:\Users\PePe\AppData\Roaming\Typora\typora-user-images\image-20211008211704602.png)]](https://img.uj5u.com/2021/10/10/272231100752476.png)
- 重啟各個服務,測驗訪問search.achangmall.com,是否會被2次轉發到search服務
成功!!!!
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-6Q5NW4Cx-1633703383270)(C:\Users\PePe\AppData\Roaming\Typora\typora-user-images\image-20211008212224949.png)]](https://img.uj5u.com/2021/10/10/272231100752477.png)
- 修改html.html為list.html
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-hxZDrkE7-1633703383271)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008213050532.png)]](https://img.uj5u.com/2021/10/10/272231100752478.png)
- 頁面跳轉的控制器com.achang.achangmall.search.controller.SearchController
@Controller
public class SearchController {
@GetMapping("/list.html")
public String listPage(){
return "list";
}
}
- 修改achangmall-product/src/main/resources/templates/index.html
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-jI8ygh3J-1633703383273)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008214911010.png)]](https://img.uj5u.com/2021/10/10/272231100752479.png)
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-e2cQJbCG-1633703383275)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008214929722.png)]](https://img.uj5u.com/2021/10/10/2722311007524710.png)
- 修改/mydata/nginx/html/static/index/js/catalogLoader.js
跳轉域名修改為你設定的域名
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-jHjv3mEz-1633703383276)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008214417851.png)]](https://img.uj5u.com/2021/10/10/2722311007524711.png)
- 測驗搜索框搜索,是否跳轉,成功
![[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ChpOayKS-1633703383277)(C:/Users/PePe/AppData/Roaming/Typora/typora-user-images/image-20211008215110934.png)]](https://img.uj5u.com/2021/10/10/2722311007524712.png)
一、檢索
檢索引數VO與url
- 全文檢索:skuTitle-》keyword
- 排序:saleCount(銷量)、hotScore(熱度分)、skuPrice(價格)
- 過濾:hasStock、skuPrice區間、brandId、catalog3Id、attrs
- 聚合:attrs
keyword=小米&
sort=saleCount_desc/asc&
hasStock=0/1&
skuPrice=400_1900&
brandId=1&
catalog3Id=1&
attrs=1_3G:4G:5G&
attrs=2_驍龍845&
attrs=4_高清屏
- 創建SearchParam用于檢索VO
//頁面檢索條件
@Data
public class SearchParam {
private String keyword;//全文匹配關鍵字
private Long catalog3Id;//三級分類id
/**
* sort=saleCount_asc/desc
* sort=skuPrice_asc/desc
* sort=hotScore_asc/desc
*/
private String sort;//排序條件
private Integer hasStock;//是否只顯示有貨
private String skuPrice;//加個區間查詢
private List<Long> brandId;//品牌id,可多選
private List<String> attrs;//按照屬性篩選
private Integer pageNum;//頁碼
}
檢索結果VO
查詢得到商品、總記錄數、總頁碼
品牌list用于在品牌欄顯示,分類list用于在分類欄顯示
其他欄每欄用AttrVo表示
不僅要根據關鍵字從es中檢索到商品
還要通過聚合生成品牌等資訊,方便分類欄顯示
- 檢索結果VO
//檢索回傳頁面Bean
@Data
public class SearchResult {
private List<SkuEsModel> products;//ES資料結構查詢到的商品資訊
private Integer pageNum;//當前頁碼
private Long total;//總記錄數
private Integer totalPage;//總頁碼
private List<BrandVo> brandVos;//當前查詢涉及到的品牌資訊
private List<AttrVo> attrVos;//當前查詢涉及到的屬性資訊
private List<CatalogVo> catalogVos;//當前查詢涉及到的分類資訊
//=========以上返給頁面的資訊=============
//品牌vo物件
public static class BrandVo{
private Integer brandId;
private String brandName;
private String brandImg;
}
//分類vo物件
public static class CatalogVo{
private Integer catalogId;
private String catalogName;
}
//屬性vo物件
public static class AttrVo{
private Integer attrId;
private String attrName;
private List<String> attrValues;
}
}
- com.achang.achangmall.search.controller.SearchController
@Controller
public class SearchController {
@Autowired
MallSearchService mallSearchService;
@GetMapping("/list.html")
public String listPage(@RequestParam SearchParam param, Model model ){
SearchResult result = mallSearchService.search(param);
model.addAttribute("result",result);
return "list";
}
}
- com.achang.achangmall.search.service.impl.MallSearchServiceImpl
@Service
public class MallSearchServiceImpl implements MallSearchService {
@Resource
private RestHighLevelClient client;
@Override
public SearchResult search(SearchParam param) {
SearchResult result = null;
SearchRequest request = buildSearchRequest(param);
try {
SearchResponse response = client.search(request, ElasticsearchConfig.COMMON_OPTIONS);
result = buildSearchResult(response);
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//構建結果資料
private SearchResult buildSearchResult(SearchResponse response) {
return null;
}
//準備檢索請求
private SearchRequest buildSearchRequest(SearchParam param) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//構建dsl陳述句
//查詢條件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
String keyword = param.getKeyword();
if (!StringUtils.isEmpty(keyword)){
boolQueryBuilder.must(QueryBuilders.matchQuery("skuTitle",keyword));
}
Long catalog3Id = param.getCatalog3Id();
if (!StringUtils.isEmpty(catalog3Id)){
boolQueryBuilder.filter(QueryBuilders.termQuery("catalogId",catalog3Id));
}
List<Long> brandId = param.getBrandId();
if (!StringUtils.isEmpty(brandId) && brandId.size()>0){
boolQueryBuilder.filter(QueryBuilders.termsQuery("brandId",brandId));
}
Integer hasStock = param.getHasStock();
boolQueryBuilder.filter(QueryBuilders.termQuery("hasStock",hasStock==1));
List<String> attrs = param.getAttrs();
if (!StringUtils.isEmpty(attrs)&&attrs.size()>0){
for (String attr : attrs) {
BoolQueryBuilder nestedBootQuery = QueryBuilders.boolQuery();
String[] attrStr = attr.split("_");
String attrId = attrStr[0];
String[] attrValues = attrStr[1].split(":");
nestedBootQuery.must(QueryBuilders.termQuery("attrs.attrId",attrId));
nestedBootQuery.must(QueryBuilders.termsQuery("attrs.attrValue",attrValues));
NestedQueryBuilder nestedQuery = QueryBuilders.nestedQuery("attrs", nestedBootQuery, ScoreMode.None);
boolQueryBuilder.filter(nestedQuery);
}
}
String skuPrice = param.getSkuPrice();
if (!StringUtils.isEmpty(skuPrice)){
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("skuPrice");
String[] strArr = skuPrice.split("_");
if (strArr.length==2){
rangeQuery.gte(strArr[0]).lte(strArr[1]);
}else if (strArr.length==1){
if (skuPrice.startsWith("_")){
rangeQuery.lte(strArr[1]);
}
if (skuPrice.endsWith("_")){
rangeQuery.gte(strArr[0]);
}
}
boolQueryBuilder.filter(rangeQuery);
}
searchSourceBuilder.query(boolQueryBuilder);
//排序,分頁,高亮
//聚合分析
SearchRequest request = new SearchRequest(new String[]{ESConstant.PRODUCT_INDEX},searchSourceBuilder);
return request;
}
}
明天繼續!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307201.html
標籤:其他
下一篇:正向代理、反向代理和負載均衡
