Roee Gavirel
Roee Gavirel

Reputation: 19445

Neo4j - Cypher: How to get random pairs of permotations

I have nodes of type A and nodes of type B.
I want to match X nodes of A and Y nodes of B and create relationship between all those A's and all those B's

Was I tried so far was (X=2, Y=2):

MATCH (a:A) WITH a,rand() AS rand1 ORDER BY rand1 desc LIMIT 2
MATCH (b:B) WITH a,b,rand() AS rand2 ORDER BY rand2 desc LIMIT 2
CREATE (a)-[i:IN]->(b)
RETURN a,i,b

But that only create 1 connection for each selected A and B.
For example:

a1-IN->b1  
a2-IN->b2

I want to get to a point where I create all permutations:

a1-IN->b1  
a1-IN->b2
a2-IN->b1  
a2-IN->b2

Any ideas?

Upvotes: 1

Views: 104

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

How about this?

MATCH (a:A) WITH a, rand() AS rand ORDER BY rand LIMIT 2
WITH collect(a) AS as

MATCH (b:B) WITH b, rand() AS rand ORDER BY rand LIMIT 2
WITH as, collect(b) AS bs

UNWIND as AS a
UNWIND bs AS b

CREATE (a)-[i:IN]->(b)
RETURN a,i,b

Upvotes: 2

Related Questions