Neo4j中的多路径关系检查

时间:2016-06-16 22:56:46

标签: neo4j cypher graph-traversal

我正在搞乱Neo4j查询。是否可以用Neo4j Cypher语言检查“n1”与标签“p”的所有关系是否都有“n6”的路径?看图像。

enter image description here

2 个答案:

答案 0 :(得分:2)

当然有可能:

MATCH (n6:Node {name:"n6"})
MATCH (n1:Node {name:"n1"})-[r:p]->()
WITH n1, n6, collect(r) as pRels
RETURN ALL(x IN pRels WHERE shortestPath( (n1)-[*]-(n6) ) )

这将返回true或false

答案 1 :(得分:1)

// End node
MATCH (n6 {name:"n6"})
// Start node and neighbors
MATCH (n1 {name:"n1"})-[:p]-(n)
// Shortest paths through the neighbor to the end node,
OPTIONAL MATCH p = shortestPath( (n)-[*]-(n6) )
    // which does not pass through the starting node
    WHERE NOT n1 IN nodes(p)
WITH
     size( collect(distinct n) ) as neighborsCount, 
     count(p) as neighborsWithPathCount
RETURN neighborsCount = neighborsWithPathCount AND 
       neighborsWithPathCount > 0