es中保存的数据类型
{"attrs" : [{"attrId" : 1,"attrName" : "入网型号","attrValue" : "a13"},{"attrId" : 2,"attrName" : "上市年份","attrValue" : "2019"},{"attrId" : 13,"attrName" : "机身颜色","attrValue" : "蓝色"},{"attrId" : 14,"attrName" : "机身长度","attrValue" : "20cm"},{"attrId" : 15,"attrName" : "机身材质工艺","attrValue" : "碳纤维"},{"attrId" : 16,"attrName" : "cpu品牌","attrValue" : "麒麟980"},{"attrId" : 17,"attrName" : "cpu型号","attrValue" : "F117S"}],"brandId" : 1,"brandImg" : "https:\\gulimall-liyuhong.oss-cn-shanghai.aliyuncs.com/2021-04-23/c13d3f54-208b-44d0-88f8-938174169611_62b97e5188766c42.png","brandName" : "华为","catelogId" : 225,"catelogName" : "手机","hasStock" : true,"hotScore" : 0,"saleCount" : 0,"skuId" : 65,"skuImg" : "https:\\gulimall-liyuhong.oss-cn-shanghai.aliyuncs.com/2021-04-23//4ac277f8-b419-4750-8943-9c5ce532dc8c_亮黑色.jpg","skuPrice" : 6499.0,"skuTitle" : "华为 亮黑色 8+128GB","spuId" : 40}
bool查询
#查询sku标题有vivo的且skuTitle中包含8GB+128GB的数据GET gulimall_product/_search{"query": {"bool": {"must": [{"match": {"skuTitle": "8GB+128GB"}},{"match": {"brandName": "vivo"}}]}}}
#过滤出分类名称是手机的, 价格是5998或者4498的数据GET gulimall_product/_search{"query": {"bool": {"filter": [{"term": {"catelogName": "手机"}},{"terms": {"skuPrice": ["5998.0","4498.0"]}},{"range": {"skuPrice": {"gte": 4000,"lte": 6000}}}]}}}
#过滤出价格在4000 ~ 6000的所有数据GET gulimall_product/_search{"query": {"bool": {"filter": [{"range": {"skuPrice": {"gte": 4000,"lte": 6000}}}]}}}
#查询出1号属性是a13或者b14的, 并且2号属性是2018的所有数据GET gulimall_product/_search{"query": {"bool": {"filter": [{"nested": {"path": "attrs","query": {"bool": {"must": [{"term": {"attrs.attrId": {"value": "1"}}},{"terms": {"attrs.attrValue": ["a13","b14"]}}]}}}},{"nested": {"path": "attrs","query": {"bool": {"must": [{"term": {"attrs.attrId": {"value": "2"}}},{"term": {"attrs.attrValue": "2018"}}]}}}}]}}}
排序和高亮
#按照skuPrice升序排列, 并且将skuTitle中所指定匹配的字段高亮GET gulimall_product/_search{"query": {"match": {"skuTitle": "vivo"}},"sort": [{"skuPrice": {"order": "asc"}}],"highlight": {"fields": {"skuTitle": {}},"pre_tags": "<b style='color:red'>","post_tags": "</b>"}}
聚合分析
#聚合出所有的品牌id, 并且子聚合出每个id对应的品牌名称和图片GET gulimall_product/_search{"aggs": {"brand_agg": {"terms": {"field": "brandId"},"aggs": {"brand_name_agg": {"terms": {"field": "brandName"}},"brand_img_agg": {"terms": {"field": "brandImg"}}}}}}
#聚合分析出所有的属性id以及对应的属性名称和值, 因为属性存在很多个,#是一个数组, 所以采用nested嵌入式聚合GET gulimall_product/_search{"aggs": {"attr_agg": {"nested": {"path": "attrs"},"aggs": {"attr_id_agg": {"terms": {"field": "attrs.attrId"},"aggs": {"attr_name_agg": {"terms": {"field": "attrs.attrName"}},"attr_value_agg": {"terms": {"field": "attrs.attrValue"}}}}}}}}
