cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Avoid Neo4J to repeat N times the same process when building node clusters

Hi there,

I have a graph with football players  (player) that are linked by the relationship "SAME_TEAM". To rebuild a team I start by a random (player) node, then run the apoc.path.expand() function to yield all the (players) in the same team:

MATCH (p:player)

CALL apoc.path.expand(p, {relationshipFilter: "SAME_TEAM", labelFilter: "player", uniqueness: "NODE_GLOBAL"}) YIELD node

WITH p, apoc.coll.sortNodes(collect(node),"name") as team

RETURN DISTINCT team

 

The major issue is that Neo4J is building each cluster N times, where N is the number of players in the team. Is there a way to skip any player nodes that have already been picked for a team so I can divide by N the number of computations ?

Thanks

5 REPLIES 5

If you data model consisted of Team and Player modes, where each player is related to their team with a MEMBER_OF relationship, you could easily find the members of each team. 

is your data model just Player modes? 

Hi glilienfiled - this is just for an example. My data model is about persons that have a specific relationship but that cannot be unilaterally linked to a « team ». 

Ok, assuming you just have collections of connected nodes that you want to get the node groups. 

Unfortunately, cypher is not a programming language. It doesn’t have the capability to iterate and update some state, which can be used to control execution. 

that being said, you could try to use apoc.perodic.commit to implement something.  This method repeats a query until the result of the query returns zero records. You could create a node that maintains state and accumulates the results during each iteration. I think it would work, but would be a bit complicated. 

https://neo4j.com/labs/apoc/4.3/overview/apoc.periodic/apoc.periodic.commit/

you can easily do what you want if you are executing your query using a driver. In that case you can maintain state in your program and repeatedly execute the query passing the state, until the query returns no result. 

That’s actually a pretty good idea, I will try it. Thanks !

Hi @ManuWeavit - @glilienfield is (I think) leading you in the right direction. Can you share more about your data model and why you cannot create the "team" and the relationships?

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online