本文以供应商与产品的关系为例,一个供应商可以有多个产品.所以以供应商为父级产品为子级来建立数据结构关系.
#建立供应商与产品的关联关系为父子级关系
PUT bd_index
{
"mappings": {
"_doc":{
"properties":{
"join_field":{
"type":"join",
"relations":{
"supplier":"product"
}
}
}
}
}
}
# 插入供应商数据
PUT bd_index/_doc/1?refresh
{
"name": "供应商1",
"join_field": "supplier"
}
# 添加产品记录
PUT bd_index/_doc/2?routing=1&refresh
{
"name": "产品1",
"join_field": {
"name": "product",
"parent": "1"
}
}
PUT bd_index/_doc/3?routing=1&refresh
{
"name": "产品2",
"join_field": {
"name": "product",
"parent": "1"
}
}
#查询供应商并返回产品
GET bd_index/_search
{
"query": {
"has_child": {
"type": "product",
"query": {
"match": {
"name": "产品1"
}
},
"inner_hits": {}
}
}
}