0
votes

I have a graph with about 800k nodes and I want to create random relationships among them, using Cypher.

Examples like the following didn't work because the cartesian product is too big:

match (u),(p)
with u,p
create (u)-[:LINKS]->(p);

For example I want 1 relationship for each node (800k), or 10 relationships for each node (8M).

In short, I need a query Cypher in order to UNIFORMLY create relationships between nodes. Does someone know the query to create relationships in this way?

2
No, the solution proposed falls back into the not working example I wrote in my question.Edoardo Basili

2 Answers

0
votes

So you want every node to have exactly x relationships? Try this in batches until no more relationships are updated:

MATCH (u),(p) WHERE size((u)-[:LINKS]->(p)) < {x}
WITH u,p LIMIT 10000 WHERE rand() < 0.2 // LIMIT to 10000 then sample
CREATE (u)-[:LINKS]->(p)
0
votes

This should work (assuming your neo4j server has enough memory):

MATCH (n)
WITH COLLECT(n) AS ns, COUNT(n) AS len
FOREACH (i IN RANGE(1, {numLinks}) |
  FOREACH (x IN ns |
    FOREACH(y IN [ns[TOINT(RAND()*len)]] |
      CREATE (x)-[:LINK]->(y) )));

This query collects all nodes, and uses nested loops to do the following {numLinks} times: create a LINK relationship between every node and a randomly chosen node.

The innermost FOREACH is used as a workaround for the current Cypher limitation that you cannot put an operation that returns a node inside a node pattern. To be specific, this is illegal: CREATE (x)-[:LINK]->(ns[TOINT(RAND()*len)]).