我发现了一个“朋友的朋友”遍历查询,满足了我获取朋友的朋友的基本需求(ArangoDB 3.0 中的朋友的朋友查询)。我对其进行了稍微修改,使其与文档中找到的“Knows_Graph”示例一起使用,并且我添加了一些额外的人员(https://docs.arangodb.com/3.11/graphs/example-graphs/#knows-图)
我的查询使用 Dave 作为起点,并通过 Bob 和 Lucy 寻找他的二级朋友。该脚本工作正常,但我也希望它在查询结果中包含一级朋友(鲍勃和露西)。我可以通过将最小深度设置为 1 来做到这一点,但是如果我这样做,我不会区分 Bob 和 Lucy 的深度(请参阅 json 输出),因此我将无法在更大的图表上判断谁是 1 度,谁是二级
最小深度为 2 的示例代码
LET person = DOCUMENT("persons/@persons/dave")
LET foaf = (
FOR v IN 2..2 ANY person knows
RETURN v
)
RETURN MERGE(person, { foaf } )
该示例使用“knows”边缘集合和“person”集合
如果我的最小深度为 1,最大深度为 2,这里是 json 输出。如您所见,bob 和 lucy 与其他人显示在相同的集合级别上。我想找到一种方法以某种方式将它们显示为第一级别,而又不会使查询过于复杂或导致性能问题。
[
{
"_id": "persons/dave",
"_key": "dave",
"_rev": "_U7wRsJG--_",
"name": "Dave",
"foaf": [
{
"_key": "lucy",
"_id": "persons/lucy",
"_rev": "_U8--wPq---",
"name": "Lucy"
},
{
"_key": "henry",
"_id": "persons/henry",
"_rev": "_U8-_FNa---",
"name": "Henry"
},
{
"_key": "bob",
"_id": "persons/bob",
"_rev": "_U7wRsJC--_",
"name": "Bob"
},
{
"_key": "charlie",
"_id": "persons/charlie",
"_rev": "_U7wRsJG---",
"name": "Charlie"
},
{
"_key": "alice",
"_id": "persons/alice",
"_rev": "_U7wRsJC---",
"name": "Alice"
},
{
"_key": "eve",
"_id": "persons/eve",
"_rev": "_U7wRsJG--A",
"name": "Eve"
}
]
}
]
通过以下查询,您可以获得每个朋友的深度信息。
LET person = DOCUMENT("persons/@persons/dave")
LET foaf = (
FOR v, e, p IN 1..2 ANY person knows
RETURN {v, depth: LENGTH(p.edges)}
)
RETURN MERGE(person, { foaf } )
输出应如下所示:
[
{
"_id": "persons/dave",
"_key": "dave",
"_rev": "_U8e0Ube---",
"foaf": [
{
"v": {
"_key": "lucy",
"_id": "persons/lucy",
"_rev": "_U8e0Y2u---"
},
"depth": 1
},
{
"v": {
"_key": "bob",
"_id": "persons/bob",
"_rev": "_U8ezwCm---"
},
"depth": 1
},
{
"v": {
"_key": "charlie",
"_id": "persons/charlie",
"_rev": "_U8e0-Oe---"
},
"depth": 2
},
{
"v": {
"_key": "eve",
"_id": "persons/eve",
"_rev": "_U8e0loy---"
},
"depth": 2
},
{
"v": {
"_key": "alice",
"_id": "persons/alice",
"_rev": "_U8e0ptu---"
},
"depth": 2
}
]
}
]
如果您想将各个深度的朋友分组在一起,您的查询可能如下所示:
LET person = DOCUMENT("persons/@persons/dave")
LET foaf = (
FOR v, e, p IN 1..2 ANY person knows
COLLECT depth = LENGTH(p.edges) INTO s KEEP v
RETURN {depth, foaf: s[*].v}
)
RETURN MERGE(person, { foaf } )
输出应如下所示:
[
{
"_id": "persons/dave",
"_key": "dave",
"_rev": "_U8e0Ube---",
"foaf": [
{
"depth": 1,
"foaf": [
{
"_key": "lucy",
"_id": "persons/lucy",
"_rev": "_U8e0Y2u---"
},
{
"_key": "bob",
"_id": "persons/bob",
"_rev": "_U8ezwCm---"
}
]
},
{
"depth": 2,
"foaf": [
{
"_key": "charlie",
"_id": "persons/charlie",
"_rev": "_U8e0-Oe---"
},
{
"_key": "eve",
"_id": "persons/eve",
"_rev": "_U8e0loy---"
},
{
"_key": "alice",
"_id": "persons/alice",
"_rev": "_U8e0ptu---"
}
]
}
]
}
]
更新
如果您需要有关人与人之间关系的更多信息,您可以返回路径而不是顶点。路径 (
p
) 包括所有访问过的边(知道关系)和所有顶点(人)。
有关 AQL 遍历语法的更多信息:请参阅 docs。
LET person = DOCUMENT("persons/@persons/dave")
LET foaf = (
FOR v, e, p IN 1..2 ANY person knows
COLLECT depth = LENGTH(p.edges) INTO s KEEP p
RETURN {depth, foaf: s[*].p}
)
RETURN MERGE(person, { foaf } )