es中保存的数据类型

    1. {
    2. "attrs" : [
    3. {
    4. "attrId" : 1,
    5. "attrName" : "入网型号",
    6. "attrValue" : "a13"
    7. },
    8. {
    9. "attrId" : 2,
    10. "attrName" : "上市年份",
    11. "attrValue" : "2019"
    12. },
    13. {
    14. "attrId" : 13,
    15. "attrName" : "机身颜色",
    16. "attrValue" : "蓝色"
    17. },
    18. {
    19. "attrId" : 14,
    20. "attrName" : "机身长度",
    21. "attrValue" : "20cm"
    22. },
    23. {
    24. "attrId" : 15,
    25. "attrName" : "机身材质工艺",
    26. "attrValue" : "碳纤维"
    27. },
    28. {
    29. "attrId" : 16,
    30. "attrName" : "cpu品牌",
    31. "attrValue" : "麒麟980"
    32. },
    33. {
    34. "attrId" : 17,
    35. "attrName" : "cpu型号",
    36. "attrValue" : "F117S"
    37. }
    38. ],
    39. "brandId" : 1,
    40. "brandImg" : "https:\\gulimall-liyuhong.oss-cn-shanghai.aliyuncs.com/2021-04-23/c13d3f54-208b-44d0-88f8-938174169611_62b97e5188766c42.png",
    41. "brandName" : "华为",
    42. "catelogId" : 225,
    43. "catelogName" : "手机",
    44. "hasStock" : true,
    45. "hotScore" : 0,
    46. "saleCount" : 0,
    47. "skuId" : 65,
    48. "skuImg" : "https:\\gulimall-liyuhong.oss-cn-shanghai.aliyuncs.com/2021-04-23//4ac277f8-b419-4750-8943-9c5ce532dc8c_亮黑色.jpg",
    49. "skuPrice" : 6499.0,
    50. "skuTitle" : "华为 亮黑色 8+128GB",
    51. "spuId" : 40
    52. }
    1. bool查询

      1. #查询sku标题有vivo的且skuTitle中包含8GB+128GB的数据
      2. GET gulimall_product/_search
      3. {
      4. "query": {
      5. "bool": {
      6. "must": [
      7. {
      8. "match": {
      9. "skuTitle": "8GB+128GB"
      10. }
      11. },
      12. {
      13. "match": {
      14. "brandName": "vivo"
      15. }
      16. }
      17. ]
      18. }
      19. }
      20. }
      1. #过滤出分类名称是手机的, 价格是5998或者4498的数据
      2. GET gulimall_product/_search
      3. {
      4. "query": {
      5. "bool": {
      6. "filter": [
      7. {
      8. "term": {
      9. "catelogName": "手机"
      10. }
      11. },
      12. {
      13. "terms": {
      14. "skuPrice": [
      15. "5998.0",
      16. "4498.0"
      17. ]
      18. }
      19. },
      20. {
      21. "range": {
      22. "skuPrice": {
      23. "gte": 4000,
      24. "lte": 6000
      25. }
      26. }
      27. }
      28. ]
      29. }
      30. }
      31. }
      1. #过滤出价格在4000 ~ 6000的所有数据
      2. GET gulimall_product/_search
      3. {
      4. "query": {
      5. "bool": {
      6. "filter": [
      7. {
      8. "range": {
      9. "skuPrice": {
      10. "gte": 4000,
      11. "lte": 6000
      12. }
      13. }
      14. }
      15. ]
      16. }
      17. }
      18. }
      1. #查询出1号属性是a13或者b14的, 并且2号属性是2018的所有数据
      2. GET gulimall_product/_search
      3. {
      4. "query": {
      5. "bool": {
      6. "filter": [
      7. {
      8. "nested": {
      9. "path": "attrs",
      10. "query": {
      11. "bool": {
      12. "must": [
      13. {
      14. "term": {
      15. "attrs.attrId": {
      16. "value": "1"
      17. }
      18. }
      19. },
      20. {
      21. "terms": {
      22. "attrs.attrValue": [
      23. "a13",
      24. "b14"
      25. ]
      26. }
      27. }
      28. ]
      29. }
      30. }
      31. }
      32. },
      33. {
      34. "nested": {
      35. "path": "attrs",
      36. "query": {
      37. "bool": {
      38. "must": [
      39. {
      40. "term": {
      41. "attrs.attrId": {
      42. "value": "2"
      43. }
      44. }
      45. },
      46. {
      47. "term": {
      48. "attrs.attrValue": "2018"
      49. }
      50. }
      51. ]
      52. }
      53. }
      54. }
      55. }
      56. ]
      57. }
      58. }
      59. }
    2. 排序和高亮

      1. #按照skuPrice升序排列, 并且将skuTitle中所指定匹配的字段高亮
      2. GET gulimall_product/_search
      3. {
      4. "query": {
      5. "match": {
      6. "skuTitle": "vivo"
      7. }
      8. },
      9. "sort": [
      10. {
      11. "skuPrice": {
      12. "order": "asc"
      13. }
      14. }
      15. ],
      16. "highlight": {
      17. "fields": {
      18. "skuTitle": {}
      19. },
      20. "pre_tags": "<b style='color:red'>",
      21. "post_tags": "</b>"
      22. }
      23. }
    3. 聚合分析

      1. #聚合出所有的品牌id, 并且子聚合出每个id对应的品牌名称和图片
      2. GET gulimall_product/_search
      3. {
      4. "aggs": {
      5. "brand_agg": {
      6. "terms": {
      7. "field": "brandId"
      8. },
      9. "aggs": {
      10. "brand_name_agg": {
      11. "terms": {
      12. "field": "brandName"
      13. }
      14. },
      15. "brand_img_agg": {
      16. "terms": {
      17. "field": "brandImg"
      18. }
      19. }
      20. }
      21. }
      22. }
      23. }
      1. #聚合分析出所有的属性id以及对应的属性名称和值, 因为属性存在很多个,
      2. #是一个数组, 所以采用nested嵌入式聚合
      3. GET gulimall_product/_search
      4. {
      5. "aggs": {
      6. "attr_agg": {
      7. "nested": {
      8. "path": "attrs"
      9. },
      10. "aggs": {
      11. "attr_id_agg": {
      12. "terms": {
      13. "field": "attrs.attrId"
      14. },
      15. "aggs": {
      16. "attr_name_agg": {
      17. "terms": {
      18. "field": "attrs.attrName"
      19. }
      20. },
      21. "attr_value_agg": {
      22. "terms": {
      23. "field": "attrs.attrValue"
      24. }
      25. }
      26. }
      27. }
      28. }
      29. }
      30. }
      31. }