sku在es中存储结构分析

有两种存储方式:
方式一
将属性冗余字段一并保存

{
	"spuId": xxx,
	"skuId": xxx,
	"xxxx基本属性": "",
	"attrs": {
		"属性1"; "xxx",
		"属性2": "xxx"
	}
}

方式二:
将冗余字段接偶

{
	"spuId": xxx,
	"skuId": xxx,
	"xxxx基本属性": "",
}

{
	"spuId": "xx",
	"属性1"; "xxx",
	"属性2": "xxx"
}

对比两种方式
首先方式一因为将attrs冗余字段一并存储 查询时会比较快 但是浪费了一些空间
其次方式二将sku和attrs接偶 节约了一些空间 但是因为要发送两次请求 所以查询时会比较慢
时间和空间之间的矛盾 我们选择时间 也就是方式一

mapping映射



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"
      },
      "notScore": {
        "type": "long"
      },
      "brandId": {
        "type": "long"
      },
      "catelogI": {
        "type": "long"
      },
      "brandName": {
        "type": "keyword",
        "index": false,
        "doc_values": false
      },
      "brandImg": {
        "type": "keyword",
        "index": false,
        "doc_values": false
      },
      "catelogName": {
        "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"
          }
        }
      }
    }
  }
}

index: 为false表示不可作为检索条件

doc_values为false 无法基于该字段排序、聚合、在脚本中访问字段值
与上面那个不同的时 这个字段为false还是可以进行math之类的query dsl操作
price商品价格使用keyword的原因是为了避免丢失精度

Nested 类型

当不是用nested类型是 es是以下存储的 将相同类型的数据存储到数组 中

PUT my-index-000001/_doc/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}
{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}

当进行查询时发现数据不正确

GET my-index-000004/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "user.first": "Alice" }},
        { "match": { "user.last":  "Smith" }}
      ]
    }
  }
}

image
原因是进行上面保存的是数组 进行es查询是 first数组中包含Alice, first整个数组命中返回 last也一样
导致数据不对 所以要添加nested类型

posted @   RainbowMagic  阅读(182)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示