ArangoDB 图表-显示朋友的朋友(和第一级共同朋友)

6qqygrtg  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(167)

我发现了一个“朋友的朋友”遍历查询,它满足了我获取朋友的朋友的基本需求(friends of friend Query in ArangoDB 3.0)。我已经稍微修改了它,使其与文档中找到的“The Knows_Graph”示例一起工作,并且我添加了一些额外的人(https://docs.arangodb.com/3.11/graphs/example-graphs/#knows-graph)

我的查询使用Dave作为起点,并通过Bob和Lucy查找他的第二学位朋友。该脚本工作正常,但我也希望它包括第一学位的朋友(鲍勃和露西)在查询结果。我可以通过将最小深度设置为1来做到这一点,但是如果我这样做,我就不会区分Bob和Lucy的深度(参见json输出),所以我无法在更大的图上分辨谁是第一级,谁是第二级
最小深度为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”集合
这里是json输出,如果我有一个最小深度为1,最大深度为2。正如您所看到的,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"
      }
    ]
  }
]
eh57zj3b

eh57zj3b1#

通过以下查询,您可以获得每个朋友的深度信息。

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 } )

相关问题