Neo4J路径枚举

时间:2017-07-22 12:07:49

标签: neo4j cypher

目前正在分析两个节点之间的不同路径。为此,我需要某种方法来唯一地识别Neo4J返回的路径中的路径。

测试用例:

#MATCH (n:City) DETACH DELETE n;
CREATE (n:City { name: 'Rome' });
CREATE (n:City { name: 'London' });
CREATE (n:City { name: 'Berlin' });
CREATE (n:City { name: 'Madrid' });
MATCH (n:City),(m:City)
WHERE n.name = 'London' AND m.name = 'Madrid'
CREATE (n)-[r:Connected]->(m) RETURN r;
MATCH (n:City),(m:City)
WHERE n.name = 'Madrid' AND m.name = 'Berlin'
CREATE (n)-[r:Connected]->(m) RETURN r;
MATCH (n:City),(m:City)
WHERE n.name = 'Berlin' AND m.name = 'Rome'
CREATE (n)-[r:Connected]->(m) RETURN r;
MATCH (n:City),(m:City)
WHERE n.name = 'London' AND m.name = 'Berlin'
CREATE (n)-[r:Connected]->(m) RETURN r;
MATCH (n:City),(m:City) WHERE n.name = 'London' AND m.name = 'Rome'
CREATE (n)-[r:Express]->(m) RETURN r;

以下查询正常

MATCH (n:City),(m:City)
WHERE n.name = 'London' AND m.name = 'Rome'
with n,m
match p= (n)-[*1..]->(m)
return p;

+---------------------------------------------------------------------------------------------------------------------------------------------+
| p                                                                                                                                                      |
+---------------------------------------------------------------------------------------------------------------------------------------------+
| (:City {name: "London"})-[:Connected]->(:City {name: "Madrid"})<-[:Connected]-(:City {name: "Berlin"})-[:Connected]->(:City {name: "Rome"}) |
| (:City {name: "London"})-[:Connected]->(:City {name: "Berlin"})<-[:Connected]-(:City {name: "Rome"})                                        |
| (:City {name: "London"})-[:Express]->(:City {name: "Rome"})                                                                                 |
+---------------------------------------------------------------------------------------------------------------------------------------------+

为了获取访问过的节点,我使用了查询:

MATCH (n:City),(m:City)
WHERE n.name = 'London' AND m.name = 'Rome'
with n,m
match p= (n)-[*1..]->(m)
unwind nodes(p) as n1 return n1;

+--------------------------+
| n1                       |
+--------------------------+
| (:City {name: "London"}) |
| (:City {name: "Madrid"}) |
| (:City {name: "Berlin"}) |
| (:City {name: "Rome"})   |
| (:City {name: "London"}) |
| (:City {name: "Berlin"}) |
| (:City {name: "Rome"})   |
| (:City {name: "London"}) |
| (:City {name: "Rome"})   |
+--------------------------+

但是,我需要一个属性来确定获取节点的路径,例如

+--------------------------+---------+
| n1                       | pathid  |
+--------------------------+---------+
| (:City {name: "London"}) | 1       |
| (:City {name: "Madrid"}) | 1       |
| (:City {name: "Berlin"}) | 1       |
| (:City {name: "Rome"})   | 1       |
| (:City {name: "London"}) | 2       |
| (:City {name: "Berlin"}) | 2       |
| (:City {name: "Rome"})   | 2       |
| (:City {name: "London"}) | 3       |
| (:City {name: "Rome"})   | 3       |
+--------------------------+---------+

我尝试使用cypher foreachreduce运算符但没有成功。能否请你就我如何列举路径提供一些指示?

由于 肯尼斯

1 个答案:

答案 0 :(得分:2)

我认为这样的事情可能适合你。

MATCH (n:City),(m:City)
WHERE n.name = 'London' AND m.name = 'Rome'
WITH n,m
MATCH p= (n)-[*1..]->(m)
WITH COLLECT(p) as paths
UNWIND RANGE(0,size(paths)-1) AS idx
UNWIND NODES(paths[idx]) AS city
RETURN city, idx

如果你想把它放在一点点饮食上,你也可以重写这样的测试数据。

CREATE (n1:City { name: 'Rome' })
CREATE (n2:City { name: 'London' })
CREATE (n3:City { name: 'Berlin' })
CREATE (n4:City { name: 'Madrid' })
CREATE (n2)-[:Connected]->(n4)
CREATE (n4)-[:Connected]->(n3) 
CREATE (n3)-[:Connected]->(n1) 
CREATE (n2)-[:Connected]->(n3)
CREATE (n2)-[:Express]->(n1);