问题描述
我将数据表示为列表(CSV), 我解析此列表并将元素(不同种类)插入dgraph中, 然后我要查询具有相互关系的Elements。
让我尝试解释一下,瓶盖与
- 品牌:A,高度:10,瓶子ID:1,类型:瓶子
- 品牌:B,高度:12,瓶子ID:2,类型:瓶子
- 品牌:C,重量:1,瓶子ID:1,类型:瓶盖
我有以下架构:
brand: string @index(hash).
bottleID: int @index(int) .
weight: int.
height: int.
cap: [uid] @ reverse.
type bottle {
brand
height
bottleID
cap
}
type cap {
brand
weight
bottleID
}
我的元素保留为:
[{
"dgraph.type": "bottle","height":10,"bottleID":1,"brand": "A"
},{
"dgraph.type": "bottle","height":12,"bottleID":2,"brand": "B"
},{
"dgraph.type": "cap","weight":1,"brand": "C"
}]
我想作为瓶子的查询响应
[{
"dgraph.type" : "bottle","brand": "A","cap": {
"dgraph.type" : "cap","brand": "C"
}
},{
"dgraph.type" : "bottle","brand": "B"
}]
要达到此目的,我查询:
{
var (func: type("bottle")) {
A as bottleID
uid
},caps(func: type("cap"),orderasc: bottleID)@filter(eq(bottleID,val(A))) {
B as bottleID
uid
},bottles(func: type("bottle"),orderasc: bottleID) @filter(eq(bottleID,val(B))){
bottleID
uid
}
}
这让我回来
"caps": [{
"bottleID": 1,"uid": "0x25185"
}],"bottles": [{
"bottleID": 1,"uid": "0x25183"
}]
然后我遍历到列表并添加:
{"set":[ {
"uid": "0x25183","cap" : {
"uid": "0x25185"
}
}]}
每对。
然后我可以查询:
{
bottles(func: type("bottle")) {
brand,height,cap {
brand
weight
}
}
}
然后我得到了想要的东西:
"bottles": [
{
"brand": "A","height": 10,"cap": {
"brand": "C","weight": 1
}
},{
"brand": "B","height": 12
}
]
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)