Neo4j - Cypher:如何获得随机的permotations对

时间:2015-11-16 13:48:55

标签: neo4j cypher

我有类型A的节点和类型B的节点 我想匹配B的A和Y节点的X节点,并在所有那些A和所有那些B之间建立关系

到目前为止我是否尝试过(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

但是,每个选定的A和B只能创建1个连接 例如:

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

我希望能够创建所有排列:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

这个怎么样?

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