我有下面的图表(在neo4j控制台上尝试:try it in the neo4j console)。从中间开始,如何编写一个Cypher查询来限制结果仅包括每个2级节点中的一个,例如仅返回红色节点?
我假设你想在第二层级别上随机选择。在这种情况下,以下Cypher语句可以完成任务:
START n=node:node_auto_index("name:start")
MATCH (n)-[:link]->(first)
WITH first
MATCH first-[:link]->(second)
WITH first, collect(second) AS coll
RETURN first, coll[toInt(length(coll)*rand())]
collect
将二级节点放入每个first
节点的集合中。使用数组下标运算符,我们随机选择其中一个元素。rand()
返回0到<1之间的值,因此我们需要乘以集合长度。
MATCH (n)-[:link]->(first), first-[:link]->(second)
。 - ulkas