3.4.2.5.Object数据类型
创作人:李增胜
在某些业务下,设计索引 Mapping 时,需要设计的对象中包含对象(非数组),此时就可以使用 Object 类型来存储对象。
以下定义了店铺对象,包含店铺名称、店铺编码、供应商信息,另外供应商信息中又包含供应商编码、供应商名称,同时供应商信息还包含自身的对象属性所在区域,所在区域又包含省和市,这种定义才能满足查询店铺信息、查询供应商所有店铺信息,以及查询某地区的所有店铺信息等等场景。
在如下示例中,supplier 是索引 my_shop 中的一个 Object,area 又是 supplier 的一个
在访问 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":"青岛市"}}}
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.5.Object数据类型(下) https://developer.aliyun.com/article/1230875