问题
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