添加模板页面

  1. <!-- 模板引擎 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>

将资料中的前端页面放到 search 服务模块下的 resource/templates 下;

配置请求跳转

配置 Nginx 转发

配置 Windows hosts 文件:

  1. 192.168.163.131 search.gulimall.com

检索服务 - 图1
找到 Nginx 的配置文件,编辑 gulimall.conf,将所有 *.gulimall.com 的请求都经由 Nginx 转发给网关;

  1. server {
  2. listen 80;
  3. server_name gulimall.com *.gulimall.com;
  4. ...
  5. }

然后重启 Nginx
docker restart nginx

配置网关服务转发到 search 服务

  1. - id: mall_search_route
  2. uri: lb://mall-search
  3. predicates:
  4. - Host=search.gulimall.com

配置页面跳转

配置 /list.html 请求转发到 list 模板

  1. /**
  2. * 自动将页面提交过来的所有请求参数封装成我们指定的对象
  3. *
  4. * @param param
  5. * @return
  6. */
  7. @GetMapping(value = "/list.html")
  8. public String listPage(SearchParam param, Model model, HttpServletRequest request) {
  9. return "list";
  10. }

检索功能

详细检索逻辑实现的代码请参考:Github

ES mall-product 映射 mapping修改

这里由于之前设置的映射 设置一些字段的 doc_value 为 false,导致后面聚合查询时报错!
修改完映射 mapping 要同步修改检索服务中的常量类中的 es 索引常量,二者要求对应。

  1. # 查看原来的映射规则
  2. GET gulimall_product/_mapping
  3. # 修改为新的映射 并创建新的索引,下面进行数据迁移
  4. PUT /mall_product
  5. {
  6. "mappings": {
  7. "properties": {
  8. "skuId": {
  9. "type": "long"
  10. },
  11. "spuId": {
  12. "type": "long"
  13. },
  14. "skuTitle": {
  15. "type": "text",
  16. "analyzer": "ik_smart"
  17. },
  18. "skuPrice": {
  19. "type": "keyword"
  20. },
  21. "skuImg": {
  22. "type": "keyword"
  23. },
  24. "saleCount": {
  25. "type": "long"
  26. },
  27. "hosStock": {
  28. "type": "boolean"
  29. },
  30. "hotScore": {
  31. "type": "long"
  32. },
  33. "brandId": {
  34. "type": "long"
  35. },
  36. "catalogId": {
  37. "type": "long"
  38. },
  39. "brandName": {
  40. "type": "keyword"
  41. },
  42. "brandImg": {
  43. "type": "keyword"
  44. },
  45. "catalogName": {
  46. "type": "keyword"
  47. },
  48. "attrs": {
  49. "type": "nested",
  50. "properties": {
  51. "attrId": {
  52. "type": "long"
  53. },
  54. "attrName": {
  55. "type": "keyword"
  56. },
  57. "attrValue": {
  58. "type": "keyword"
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. # 数据迁移
  66. POST _reindex
  67. {
  68. "source": {
  69. "index": "gulimall_product"
  70. },
  71. "dest": {
  72. "index": "mall_product"
  73. }
  74. }

ES 检索 DSL语句分析

  1. GET mall_product/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [ {"match": { "skuTitle": "华为" }} ], # 检索出华为
  6. "filter": [ # 过滤
  7. { "term": { "catalogId": "225" } },
  8. { "terms": {"brandId": [ "2"] } },
  9. { "term": { "hasStock": "false"} },
  10. {
  11. "range": {
  12. "skuPrice": { # 价格1K~7K
  13. "gte": 1000,
  14. "lte": 7000
  15. }
  16. }
  17. },
  18. {
  19. "nested": {
  20. "path": "attrs", # 聚合名字
  21. "query": {
  22. "bool": {
  23. "must": [
  24. {
  25. "term": { "attrs.attrId": { "value": "6"} }
  26. }
  27. ]
  28. }
  29. }
  30. }
  31. }
  32. ]
  33. }
  34. },
  35. "sort": [ {"skuPrice": {"order": "desc" } } ],
  36. "from": 0,
  37. "size": 5,
  38. "highlight": {
  39. "fields": {"skuTitle": {}}, # 高亮的字段
  40. "pre_tags": "<b style='color:red'>", # 前缀
  41. "post_tags": "</b>"
  42. },
  43. "aggs": { # 查完后聚合
  44. "brandAgg": {
  45. "terms": {
  46. "field": "brandId",
  47. "size": 10
  48. },
  49. "aggs": { # 子聚合
  50. "brandNameAgg": { # 每个商品id的品牌
  51. "terms": {
  52. "field": "brandName",
  53. "size": 10
  54. }
  55. },
  56. "brandImgAgg": {
  57. "terms": {
  58. "field": "brandImg",
  59. "size": 10
  60. }
  61. }
  62. }
  63. },
  64. "catalogAgg":{
  65. "terms": {
  66. "field": "catalogId",
  67. "size": 10
  68. },
  69. "aggs": {
  70. "catalogNameAgg": {
  71. "terms": {
  72. "field": "catalogName",
  73. "size": 10
  74. }
  75. }
  76. }
  77. },
  78. "attrs":{
  79. "nested": {"path": "attrs" },
  80. "aggs": {
  81. "attrIdAgg": {
  82. "terms": {
  83. "field": "attrs.attrId",
  84. "size": 10
  85. },
  86. "aggs": {
  87. "attrNameAgg": {
  88. "terms": {
  89. "field": "attrs.attrName",
  90. "size": 10
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }