Neo4j 中的多路径关系检查
Multiple path relation check in Neo4j
我正在处理 Neo4j 查询。是否可以使用 Neo4j Cypher 语言检查 "n1" 与标签 "p" 的所有关系是否都有通往 "n6" 的路径?看图。
当然可以:
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) ) )
这将 return 是或否
// 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
我正在处理 Neo4j 查询。是否可以使用 Neo4j Cypher 语言检查 "n1" 与标签 "p" 的所有关系是否都有通往 "n6" 的路径?看图。
当然可以:
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) ) )
这将 return 是或否
// 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