数据模型
# 数据模型知识点
## 逆天BUG解决-1
> NACOS无法发现服务, 总是显示服务未上线!
> 换微服务的名字, 重启, 然后换回来, 如果换回来出现一闪一闪的情况, 不要换回来了
## 逆天BUG解决-2
> skuPrice这个属性在es中是以keyword的形式存在的, 目的是为了规避精度上的损失, 但是这同时带来一个非常严重的问题, 就是在进行range的时候, 进行的是字符串比较, 有很多比较的结果都是不正确的
> <font color="red">**在查询价格区间的时候, 底层需要补0, 规定统一的长度为6, 不足6则在前面补0, 存储和查询的时候都要补** </font>
## 逆天BUG解决-3(想错了, 这里一个SKU有多个规格参数, 下面都是废话, 也有一定借鉴意义)
> 在查找规格参数的时候, 可能会给多个规格参数, 我们需要把这些规格参数都查询出来, 但是bool里面的所有的条件都是以and的形式连接起来的, 这样根本就不可能把多个规格参数查询出来
> <font color="red">**无可奈何花落去**</font>, 这里只能采取并集查询, 即查询多次, 虽然吞吐量下降了, 但是基于上面是完全错误的情况下, 强行给es查询条件来一个or, 导致es的查询效率变低, 无论弄多少个微服务集群哦都会慢, 而牺牲微服务的吞吐量, 开源通过更多的集群来弥补, <font color="red">**切勿因小失大**</font>
## 逆天BUG解决-4
> 在全文检索的时候, 有时候明明是标题里面的一部分, 但是无法检索出来, 本质**原因是IK分词器出现了问题**
**因为IK分词器没有对应的词语, 所以倒排索引通过分词查找的时候找不出来**
> <font color="blue">**解决方案: 添加对应的分词, 比如华找不出华为, 就把华放进去词典里面, 然后把索引删了重新构建, 差不多好了, 注意数据的备份即可如果还是不行就重启**</font>
## 逆天BUG解决-5
> **如果进行了排序, 相关性系数会丢失**
> **用不了也没办法捏**
## 1. 修改映射
> 因为之前的映射中, 一些属性是不参与检索和聚合, 但是, 由于业务的变化, 这些数据需要参与聚合, 因此需要修改映射
```kibana
PUT /bitmall_product
{
"mappings": {
"properties": {
"attrs": {
"type": "nested",
"properties": {
"attrId": {
"type": "long"
},
"attrName": {
"type": "keyword"
},
"attrValue": {
"type": "keyword"
}
}
},
"brandId": {
"type": "long"
},
"brandImg": {
"type": "keyword"
},
"brandName": {
"type": "keyword"
},
"catalogId": {
"type": "long"
},
"catalogName": {
"type": "keyword"
},
"hasStock": {
"type": "boolean"
},
"hotScore": {
"type": "long"
},
"saleCount": {
"type": "long"
},
"skuId": {
"type": "long"
},
"skuImg": {
"type": "keyword"
},
"skuPrice": {
"type": "keyword"
},
"skuTitle": {
"type": "text",
"analyzer": "ik_max_word"
},
"spuId": {
"type": "keyword"
}
}
}
}
POST _reindex
{
"source": {
"index": "product"
}
, "dest": {
"index": "bitmall_product"
}
}
DELETE product
```
## 2. QueryDSL
### 2.1 约定
> 1. 全文检索的条件都放在`must`里面, 参与相关系数的评分
> 2. 非全文检索的条件都放在`filter`里面, 不参与相关系数的评分, 提高**查询效率**
```kibana
GET /bitmall_product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "华为"
}
}
],
"filter": [
{
"term": {
"catalogId": "225"
}
},
{
"term": {
"hasStock": "false"
}
},
{
"terms": {
"brandId": [
9
]
}
},
{
"range": {
"skuPrice": {
"gte": "00011.0",
"lte": "10233.0"
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"must": [
{
"term": {
"attrs.attrId": "7"
}
},
{
"terms": {
"attrs.attrValue": [
"A2217"
]
}
}
]
}
}
}
}
]
}
},
"sort": [
{
"skuPrice": {
"order": "desc"
}
}
],
"from": 0,
"size": 20,
"highlight": {
"pre_tags": "<b style='color:red'><i>"
, "post_tags": "</i></b>"
, "fields": {
"skuTitle": {
}
}
},
"aggs": {
"brand_agg": {
"terms": {
"field": "brandId",
"size": 127
}
, "aggs": {
"brand_name_agg": {
"terms": {
"field": "brandName",
"size": 1
}
},
"brand_img_agg": {
"terms": {
"field": "brandImg",
"size": 1
}
}
}
},
"category_agg": {
"terms": {
"field": "catalogId",
"size": 127
}, "aggs": {
"category_name_agg": {
"terms": {
"field": "catalogName",
"size": 1
}
}
}
},
"attr_agg": {
"nested": {
"path": "attrs"
}
, "aggs": {
"attr_id_agg": {
"terms": {
"field": "attrs.attrId",
"size": 127
}
, "aggs": {
"attr_name_agg": {
"terms": {
"field": "attrs.attrName",
"size": 1
}
},
"attr_value_agg": {
"terms": {
"field": "attrs.attrValue",
"size": 127
}
}
}
}
}
}
}
}
```
## 其他知识点补充
### 为什么规格参数的值需要精确匹配?
> 如果规格参数来个全文匹配, 你来个枭龙, 全部型号都给你找到了, 不符合要求
### 为什么属性id不能用terms?
> 因为一个规格参数, 如果用了terms相当于多个规格参数之一, 表示或的关系, 注意一个sku有多个规格参数
### 怎么在聚合里面冗余存储?
> 如果按照品牌来说, 用terms聚合可以将不同的品牌根据id分类好, 此时我们还想存储品牌的名字, 我们就可以采取采取子聚合的方式冗余存储, 因为一个品牌只有一个名字, 因此来个terms子聚合一定可以获取到唯一的名字
### 为什么需要多个nested?