如何使用neo4j查找2个节点之间的路由?

时间:2018-05-09 10:49:22

标签: neo4j cypher nodes

所以我在cypher中有这个查询

create (London:Station {name:'London Station'})
create (Bristol:Station {name:'Bristol Station'})
create (Southampton:Station {name:'Southampton Station'})
create (Northampton:Station {name:'Northampton Station'})
create (Brighton:Station {name:'Brighton Station'})
create (Oxford:Station {name:'Oxford Station'})
create (Portsmouth:Station {name:'Portsmouth Station'})
create (Gloucester:Station {name:'Gloucester Station'})
create (London)-[:LEAD_TO {distance:95}]->(Oxford)
create (London)-[:LEAD_TO {distance:52}]->(Brighton)
create (Oxford)-[:LEAD_TO {distance:45}]->(Northampton)
create (Oxford)-[:LEAD_TO {distance:66}]->(Southampton)
create (Brighton)-[:LEAD_TO {distance:49}]->(Portsmouth)
create (Portsmouth)-[:LEAD_TO {distance:20}]->(Southampton)
create (Southampton)-[:LEAD_TO {distance:77}]->(Bristol)
create (Northampton)-[:LEAD_TO {distance:106}]->(Gloucester)
create (Northampton)-[:LEAD_TO {distance:114}]->(Bristol)
create (Gloucester)-[:LEAD_TO {distance:35}]->(Bristol)

我遇到的问题是:如何找到2个节点之间的所有路由,如节点1伦敦到节点2布里斯托尔,请帮帮我

1 个答案:

答案 0 :(得分:1)

您可以搜索这两个节点之间的路径:

MATCH p=(London:Station {name:'London Station'})-[:LEAD_TO*]->(Bristol:Station {name:'Bristol Station'})
RETURN reduce(s = "", x IN NODES(p) | s + x.name + " " ), reduce(s = 0, x IN RELATIONSHIPS(p) | s + x.distance )