· 更多精彩内容,请下载阅读全本《Elastic Stack实战手册》
创作人:李增胜
在某些业务下,设计索引 Mapping 时,需要设计的对象中包含对象(非数组),此时就可以使用 Object 类型来存储对象。
以下定义了店铺对象,包含店铺名称、店铺编码、供应商信息,另外供应商信息中又包含供应商编码、供应商名称,同时供应商信息还包含自身的对象属性所在区域,所在区域又包含省和市,这种定义才能满足查询店铺信息、查询供应商所有店铺信息,以及查询某地区的所有店铺信息等等场景。
在如下示例中,supplier 是索引 my_shop 中的一个 Object,area 又是 supplier 的一个 Object
在访问 area 时,需要通过 supplier.area 才能访问
#定义 mapping
PUT my_shop
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"shopName": {
"type": "text",
"analyzer": "ik_smart"
},
"shopCode": {
"type": "keyword"
},
"supplier": {
"properties": {
"supplier_code": {
"type": "keyword"
},
"supplier_name": {
"type": "text",
"analyzer": "ik_smart"
},
"area": {
"properties": {
"province": {
"type": "keyword"
},
"city": {
"type": "keyword"
}
}
}
}
}
}
}
}
#插入测试数据
POST my_shop/_bulk
{"index":{"_id":1}}
{"shopName":"苹果热销店铺","shopCode":"sc001","supplier":{"supplier_code":"001","supplier_name":"南京农村电商领导者","area":{"province":"江苏省","city":"南京市"}}}
{"index":{"_id":2}}
{"shopName":"美的热销店铺","shopCode":"sc002","supplier":{"supplier_code":"001","supplier_name":"南京农村电商领导者","area":{"province":"江苏省","city":"南京市"}}}
{"index":{"_id":3}}
{"shopName":"金沙酒热销店铺","shopCode":"sc003","supplier":{"supplier_code":"002","supplier_name":"山东农村电商领导者","area":{"province":"江苏省","city":"南京市"}}}
{"index":{"_id":4}}
{"shopName":"华为热销店铺","shopCode":"sc004","supplier":{"supplier_code":"002","supplier_name":"山东农村电商领导者","area":{"province":"山东省","city":"青岛市"}}}
测试数据包括2家供应商
- 南京农村电商领导者 店铺:苹果热销店铺 + 美的热销店铺
- 山东农村电商领导者 店铺:金沙酒热销店铺 + 华为热销店铺
查询供应商 001 对应的所有店铺:
#访问时,需要supplier.supplier_code指定对象对应的字段
POST my_shop/_search
{
"query": {
"match": {
"supplier.supplier_code": "001"
}
}
}
#返回
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.6931471,
"hits" : [
{
"_index" : "my_shop",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.6931471,
"_source" : {
"shopName" : "苹果热销店铺",
"shopCode" : "sc001",
"supplier" : {
"supplier_code" : "001",
"supplier_name" : "南京农村电商领导者",
"area" : {
"province" : "江苏省",
"city" : "南京市"
}
}
}
},
{
"_index" : "my_shop",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.6931471,
"_source" : {
"shopName" : "美的热销店铺",
"shopCode" : "sc002",
"supplier" : {
"supplier_code" : "001",
"supplier_name" : "南京农村电商领导者",
"area" : {
"province" : "江苏省",
"city" : "南京市"
}
}
}
}
]
}
}
#查询销售区域在南京的所有店铺
#通过 supplier.area.city 访问对应的字段值
POST my_shop/_search
{
"query": {
"match": {
"supplier.area.city": "南京市"
}
}
}
#返回
{
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.35667494,
"hits" : [
{
"_index" : "my_shop",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.35667494,
"_source" : {
"shopName" : "苹果热销店铺",
"shopCode" : "sc001",
"supplier" : {
"supplier_code" : "001",
"supplier_name" : "南京农村电商领导者",
"area" : {
"province" : "江苏省",
"city" : "南京市"
}
}
}
},
{
"_index" : "my_shop",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.35667494,
"_source" : {
"shopName" : "美的热销店铺",
"shopCode" : "sc002",
"supplier" : {
"supplier_code" : "001",
"supplier_name" : "南京农村电商领导者",
"area" : {
"province" : "江苏省",
"city" : "南京市"
}
}
}
},
{
"_index" : "my_shop",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.35667494,
"_source" : {
"shopName" : "金沙酒热销店铺",
"shopCode" : "sc003",
"supplier" : {
"supplier_code" : "002",
"supplier_name" : "山东农村电商领导者",
"area" : {
"province" : "江苏省",
"city" : "南京市"
}
}
}
}
]
}
}