简体   繁体   中英

Gremlin Java : Get Vertices by max Edge count

Suppose we have a graph like this.

(User)-[:KNOWS]->(Friend)

I want to count all outgoing relationship from User and group them by user, then add some condition to filter. (Like more than 10 Knows) This is what I did,

g.V().hasLabel("Friend").in("KNOWS").hasLabel("User").groupCount().next()

This is returning a map, so I can add the condition to filter the results. My question is , do we have any efficient alternative way to do this ?

I am not sure if I understood your question correctly, but it sounds like you just want to filter all users based on their number of outgoing edges with the label knows . In that case you can directly start at the User vertices and filter them based on the number of their KNOWS edges instead of doing a groupCount :

g.V().hasLabel('User').where(outE('KNOWS').count().is(gt(10)))

Until now I ignored any performance constrains. But as Paul Jackson mentioned in his comment it is not efficient to execute such a query in OLTP mode like this. Neo4j will probably iterate over all vertices, check whether they have the label User and then count their KNOWS edges.

You basically have two options to speed this up:

  1. As Paul Jackson suggested: Add the edge count as a property to the vertices, pre-compute it and then index this property or
  2. Use something like Spark-Gremlin if you really want to compute the edge count on the fly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM