1. 1建立一个映射索引
PUT product
{
"mappings": {
"properties": {
"skuId": {
"type": "long"
},
"spuId": {
"type": "keyword"
},
"skuTitle": {
"type": "text",
"analyzer": "ik_smart" //ik 分词器
},
"skuPrice": {
"type": "keyword"
},
"skuImg": {
"type": "keyword",
"index": false, //字段不会被索引,不能用来搜索
"doc_values": false //不排序
},
"saleCount": {
"type": "long"
},
"hasStock": {
"type": "boolean"
},
"hotScore": {
"type": "long"
},
"brandId": {
"type": "long"
},
"catelogId": {
"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", //属性为一个List<对象>,声明为 嵌套式
"properties": {
"attrId": {
"type": "long"
},
"attrName": {
"type": "keyword",
"index": false,
"doc_values": false
},
"attrValue": {
"type": "keyword"
}
}
}
}
}
}
1.2 商品的全局查询和聚合分析功能
GET product/_search
{
"query": {
"bool": {
//复合查询
"must": [
{
"match": {
//模糊查询,keyword=华为
"skuTitle": "华为"
}
}
],
"filter": [//过滤,不产生分数优化查询速度(按照属性,分类,品牌,价格区间,库存
{
//模糊检索推荐使用match -> 文本字段使用
"term": {
//精确检索推荐使用term -> 非文本字段使用
"catelogId": "225"
}
},
{
"terms": {
"brandId": [
"1",
"2",
"9"
]
}
},
{
"nested": {
//嵌套查询
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": {
"value": "15"
}
}
},
{
"terms": {
"attrs.attrValue": [
"海思(Hisilicon)",
"以官网信息为准"
]
}
}
]
}
}
}
},
{
"term": {
"hasStock": "true"
}
},
{
"range": {
"skuPrice": {
//价格区间
"gte": 0,
"lte": 6000
}
}
}
]
}
},
"sort": [ //排序 降序
{