由于电商网站商品资料品种繁多,数据量大,当用户搜索商品时,如果使用传统的Mysql数据库进行查询,项目的性能无法满足用户要求,为了提高响应速度,这时我们就要把主要的商品信息放入ES中。下图定义了一个通用的电商数据存储模版:

电商项目使用ElasticSearch定义商品索引_elasticsearch


通用Json数据结构:

PUT product
{
"mappings": {
"properties": {
"skuId": {
"type": "long"
},
"spuId": {
"type": "keyword"
},
"skuTitle": {
"type": "text",
"analyzer": "ik_smart"
},
"skuPrice": {
"type": "double"
},
"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"
}
}
}
}
}
}

attr的类型必须为nested,如果不设置为nested,就会扁平化数据,导致查询结果不准确,产生多余数据

电商项目地址:​​https://gitee.com/charlinchenlin/wysmall​