2
votes

I need to sort nodes by their out-degree. In neo4j with cypher query language, I do do something like:

MATCH (P1:P)
RETURN P1,size((P1)-->()) as degree 
ORDER BY degree DESC LIMIT 10

In gremlin, I know how to count the out degree for each node:

g.V().hasLabel('V')
    .order().by(out('E').count(), desc)
    .limit(10)

However, I don't see how to return the count as well as the node itself.

Is there any way to make query like this in gremlin?

1

1 Answers

1
votes

You can project the degree:

g.V().hasLabel('V').
  project('vertex', 'degree')
    .by(identity())
    .by(out('E').count())
    .order().by(select('degree'), desc)
    .limit(10)

example: https://gremlify.com/c3bw9gpr36o5k