Search code examples
gremlintinkerpoptinkerpop3gremlin-serveramazon-neptune

Gremlin groupcount by edges and then also select other properties besides the count itself


I'm modeling a social network where users follow others. I'm trying to generate a list of potential users to follow and order it by those who are also followed by the people I'm following (let's call these 'mutualFollowers' for lack of a better term).

My current query does this by getting all users who are followed by the users that "myusername" follows (edges "following" from userOne --> userTwo) and removing those who the user "myusername" already follows (the aggregated group "following" here).

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

I can correctly generate this list, but I am only able to get a list with entries like

username: 5 (where username is the username and 5 is the number of mutual followers)

I would also like to be able to select other properties of each user vertex like "name", "profile_pic", etc in some form similar to

{username: username, name: personName, mutualFollowers: 10}

Is there an easy way to do this?

(using AWS Neptune)


Solution

  • Without changing too much you could just project() a Map as the key in the 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)
    

    That doesn't quite get you exactly the form you asked for though but I wanted to demonstrate a way that required the least amount of change to your original query. If you want an actual mutualFollowers key in the map you will have to deconstruct your Map and flatten it:

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