neo4j: shortest paths constrained by node and rel properties

﹥>﹥吖頭↗ 提交于 2019-12-12 03:02:12

问题


From the tutorial:

MATCH p=shortestPath( (keanu:Person)-[:KNOWS*]-(kevin:Person) ) 
WHERE keanu.name="Keanu Reeves" and kevin.name = "Kevin Bacon"
RETURN length(p)

Imagine each Person node has an age property and an occupation. Every KNOWS edge has a length of acquaintance property.

What Cypher query would return a shortest path with, for instance,

age > 40 years
not (occupation = ACTOR)
lengthOfAcquaintance > 10 years

In molecular biology interaction data, we wish to make yet more complex queries - probably of the sort which cypher handles well, but just how eludes me. For example:

Find the shortest paths between receptor molecule A and transcription factor B, where most edges are from small-scale experiments, most of the genes are annotated as kinase, and evidence (weight) for the edges is greater than 0.5

These queries probably come up in lots of settings. Can anyone point me towards readings and examples which will help me understand how this is done? And maybe provide a modest extension of the "Bacon Number" query which accommodates one node and one edge property?

Thanks!

  • Paul

回答1:


I think that what you are looking for is the ALL predicate.

Here are a couple of examples using it :

ShortestPath where all actors in the paths should have an age > 40 :

MATCH p=shortestPath( (keanu:Person)-[:KNOWS*]-(kevin:Person) ) 
WHERE keanu.name = "Keanu Reeves"
AND kevin.name = "Kevin Bacon" 
AND ALL(x IN nodes(p) WHERE x.age > 40)
RETURN p

ShortestPath where all edges should have a lengthOfAcquaintance > 10

MATCH p=shortestPath( (keanu:Person)-[:KNOWS*]-(kevin:Person) ) 
WHERE keanu.name = "Keanu Reeves" 
AND kevin.name = "Kevin Bacon"
AND ALL(x in rels(p) WHERE x.lengthOfAcquaintance > 100
RETURN p

Of course you can combine the both :

MATCH p=shortestPath( (keanu:Person)-[:KNOWS*]-(kevin:Person) ) 
WHERE keanu.name = "Keanu Reeves" 
AND kevin.name = "Kevin Bacon"
AND ALL(x in rels(p) WHERE x.lengthOfAcquaintance > 10)
AND ALL(x in nodes(p) WHERE x.age > 40)
RETURN p


来源:https://stackoverflow.com/questions/35447524/neo4j-shortest-paths-constrained-by-node-and-rel-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!