【问题标题】:Gremlin groupcount by edges and then also select other properties besides the count itselfGremlin 按边分组计数,然后还选择除计数本身之外的其他属性
【发布时间】:2020-11-19 02:44:17
【问题描述】:

我正在为用户关注他人的社交网络建模。我正在尝试生成要关注的潜在用户列表,并由那些也被我关注的人关注的人订购(我们称这些为“mutualFollowers”,因为没有更好的术语)。

我当前的查询通过获取“myusername”关注的用户所关注的所有用户(来自 userOne --> userTwo 的边缘“关注”)并删除用户“myusername”已经关注的用户(聚合组“跟随”在这里)。

g.V().has("user", "username", "myusername").out("follows").aggregate("following").out("follows").where(without("following")).groupCount().by("username").order(local).by(values, decr).next()

我可以正确生成这个列表,但我只能得到一个包含类似条目的列表

username: 5(其中username是用户名,5是相互关注者的数量)

我还希望能够以类似于

的某种形式选择每个用户顶点的其他属性,例如“name”、“profile_pic”等
{username: username, name: personName, mutualFollowers: 10}

有没有简单的方法可以做到这一点?

(使用 AWS 海王星)

【问题讨论】:

    标签: gremlin tinkerpop tinkerpop3 gremlin-server amazon-neptune


    【解决方案1】:

    如果不做太多更改,您可以将project()Map 作为groupCount() 中的键:

    g.V().
      has("user", "username", "myusername").
      out("follows").
      aggregate("following").
      out("follows").
      where(without("following")).
      groupCount().
        by(project('username','name').
             by('username').by('name')).
      order(local).by(values, decr)
    

    虽然这并不能完全满足您的要求,但我想演示一种对原始查询进行最少更改的方法。如果您想要地图中的实际 mutualFollowers 键,则必须解构您的 Map 并将其展平:

    g.V().
      has("user", "username", "myusername").
      out("follows").
      aggregate("following").
      out("follows").
      where(without("following")).
      groupCount().
      order(local).by(values, decr).
      unfold().
      project('username','name','mutualFollowers').
        by(select('keys').values('username')).
        by(select('keys').values('name')).
        by(select(values))
    

    【讨论】:

    • 谢谢!也感谢您提供两种解决方案,确实使概念更加清晰
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多