(只有上架的商品存储到Elasticsearch中才能被检索)
分析-存储商品的什么信息到 es 中
1. 需要保存 sku 信息
- 当搜索商品名时,查询的是 sku 的标题 sku_title;
-
2. 需要保存品牌、分类等信息
点击分类,检索分类下的所有信息
-
3. 需要保存 spu 信息
-
分析-怎么设计存储结构来保存数据
1. 方案1-空间换时间
{skuId:1spuId:11skyTitile:华为xxprice:999saleCount:99attrs:[{尺寸:5存},{CPU:高通945},{分辨率:全高清}]}# 缺点:会产生冗余字段,对于相同类型的商品,attrs 属性字段会重复,空间占用大# 好处:方便检索
2. 方案2-时间换空间
sku索引{skuId:1spuId:11}attr索引{spuId:11attrs:[{尺寸:5寸},{CPU:高通945},{分辨率:全高清}]}# 缺点:选择公共属性attr时,会检索当前属性的所有商品分类,然后再查询当前商品分类的所有可能属性;# 导致耗时长。# 好处:空间利用率高
3. 最终方案-存储结构
完整结构:
PUT product{"mappings": {"properties": {"skuId": { "type": "long" },"spuId": { "type": "keyword" },"skuTitle": {"type": "text","analyzer": "ik_smart"},"skuPrice": { "type": "keyword" },"skuImg": {"type": "keyword","index": false,"doc_values": false},"saleCount":{ "type":"long" },"hasStock": { "type": "boolean" },"hotScore": { "type": "long" },"brandId": { "type": "long" },"catalogId": { "type": "long" },"brandName": {"type": "keyword","index": false,"doc_values": false},"brandImg":{"type": "keyword","index": false,"doc_values": false},"catalogName": {"type": "keyword","index": false,"doc_values": false},"attrs": {"type": "nested","properties": {"attrId": {"type": "long" },"attrName": {"type": "keyword","index": false,"doc_values": false},"attrValue": { "type": "keyword" }}}}}}
mapping结构字段说明:
"mappings": {"properties": {"skuId": { "type": "long" },"spuId": { "type": "keyword" }, # 精确检索,不分词"skuTitle": {"type": "text", # 全文检索"analyzer": "ik_smart" # 分词器},"skuPrice": { "type": "keyword" },"skuImg": {"type": "keyword","index": false, # false 不可被检索"doc_values": false # false 不可被聚合},"saleCount":{ "type":"long" }, # 商品销量"hasStock": { "type": "boolean" }, # 商品是否有库存"hotScore": { "type": "long" }, # 商品热度评分"brandId": { "type": "long" }, # 品牌id"catalogId": { "type": "long" }, # 分类id"brandName": { # 品牌名,只用来查看,不用来检索和聚合"type": "keyword","index": false,"doc_values": false},"brandImg":{ # 品牌图片,只用来查看,不用来检索和聚合"type": "keyword","index": false,"doc_values": false},"catalogName": { # 分类名,只用来查看,不用来检索和聚合"type": "keyword","index": false,"doc_values": false},"attrs": { # 属性对象"type": "nested", # 嵌入式,内部属性"properties": {"attrId": {"type": "long" },"attrName": { # 属性名"type": "keyword","index": false,"doc_values": false},"attrValue": { "type": "keyword" } # 属性值}}}}
4. 关于 nested 类型
Object 数据类型的数组会被扁平化处理为一个简单的键与值的列表,即对象的相同属性会放到同一个数组中,在检索时会出现错误。参考官网:How arrays of objects are flattened
- 对于 Object 类型的数组,要使用 nested 字段类型。参考官网:Using nested fields for arrays of objects
