gpt4 book ai didi

javascript - 在gremlin中,如何查找和排序给定顶点所属的所有连接顶点的三元组?

转载 作者:行者123 更新时间:2023-12-04 10:36:33 25 4
gpt4 key购买 nike

我正在制作一个社交应用程序,用户可以在其中成为 friend 。

对于给定用户 A ,我想找到所有用户的黑社会,使得 A -isFriends-> B AND B -isFriends-> C AND C -isFriends-> A .

我目前的做法如下:

g.V(A).repeat(__.out('isFriends')).times(3).path().by(id).toList()

然后在 gremlin 之外,我过滤掉第一个对象与最后一个对象不同的所有 Path 对象。我更愿意让小鬼为我做这个过滤,但我不确定如何根据 path() 的输出进行过滤.

我试过 cyclicPath() ,但这只是返回一个我不明白的 Vertex 对象的平面列表。由此,我期望与 path() 类似的输出但只包含第一个和最后一个顶点相同的路径。让我知道这种期望是否不正确。

我还想根据子遍历的结果(三个顶点有多少共同 friend )对这些路径进行排序,但我不确定如何从 path() 的输出中包含的顶点开始遍历。 ,无需启动新的 gremlin 查询。

我正在使用 javascript-gremlin 驱动程序 (v3.4.4) 并且正在对 AWS Neptune 进行查询,其中 lambdas 不可用。

如果我的方法或理解不正确,请告诉我。

最佳答案

下面是一个查询来回答你的两个问题。是优化 只运行 2 跳:

g.V().hasLabel("A").as("A").both("isFriends").as("B").aggregate("friends")
.out("isFriends").as("C").where(within("friends"))
.project("triad","mutual")
.by(select("A","B","C").by(label()))
.by(select("B","C").select(values).unfold()
.both("isFriends").where(within("friends"))
.groupCount().by().unfold()
.where(select(values).is(eq(2)))
.select(keys).label().fold())
.order().by(select("mutual").count(local), desc)

一些解释:
  • 找到一个 friend 并将他们存储为“ friend ”。
  • 然后找到他们在“ friend ”中的 friend ,因此与 A 成为 friend (这次使用 out 以防止重复)。
  • 使用 project 使结果更加冗长。
  • 选择 A、B 和 C 来获得三元组。

  • 现在来寻找三合会共同 friend 的有趣部分:
  • 从 B 和 C 开始,找到他们的 friend ,他们也是 A friend 。
  • 对这些 friend 进行分组计数并仅过滤那些计数为 2 的 friend (同时拥有 B 和 C 的 friend )。
  • 最后,按共同 friend 的数量对结果进行排序。

  • 您可以通过替换最后两行来保留他们的数量,而不是实际的共同好友列表:
        .select(keys).label().fold())
    .order().by(select("mutual").count(local), desc)

    和:
        .count())
    .order().by(select("mutual"), desc)

    最后,如果您只想要黑社会(仍然排序),您可以删除项目:
    g.V().hasLabel("A").as("A").both("isFriends").as("B").aggregate("friends")
    .out("isFriends").as("C").where(within("friends"))
    .order().by(
    select("B","C").select(values).unfold()
    .both("isFriends").where(within("friends"))
    .groupCount().by().unfold().where(select(values).is(eq(2)))
    .count(), desc)
    .select("A","B","C").by(label()).select(values)

    关于javascript - 在gremlin中,如何查找和排序给定顶点所属的所有连接顶点的三元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60156915/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com