Last active
November 29, 2019 03:33
-
-
Save vladbatushkov/f9661ef1e66dcf2eced4ae3646fe262e to your computer and use it in GitHub Desktop.
LookAt example of apoc.cypher.run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// roots | |
MATCH (n) DETACH DELETE n; | |
CREATE | |
(n1:Root:Red { id:'N1', color: "Red", size: 1000 }), | |
(n2:Root:Green { id:'N2', color: "Green", size: 2000 }), | |
(n3:Root:Blue { id:'N3', color: "Blue", size: 3000 }), | |
(n4:Root:Purple { id:'N2', color: "Purple", size: 2000 }), | |
(n5:Root:Orange { id:'N1', color: "Orange", size: 3000 }); | |
MATCH (n1 {id:'N1'}), (n2 {id:'N2'}) | |
CREATE (n1)-[:ANY { value: 1 }]->(n2); | |
MATCH (n1 {id:'N1'}), (n3 {id:'N3'}) | |
CREATE (n1)-[:ANY { value: 3}]->(n3); | |
// static-dynamic | |
WITH 3 as lookAtPattern | |
WITH [ | |
'RETURN NULL as nodes',//0 | |
'MATCH path = (:Root { id: "N1" }) RETURN nodes(path) as nodes',//1 | |
'MATCH path = (:Root { id: "N1" }) RETURN nodes(path) as nodes',//2 | |
'MATCH path = (:Root { id: "N1" })-[:ANY { value: 1 }]->(:Root { id: "N2" }) RETURN nodes(path) as nodes',//3 | |
'MATCH path = (:Root { id: "N2" })-[:ANY { value: 4 }]->(:Root { id: "N1" }) RETURN nodes(path) as nodes',//4 | |
'MATCH path = (:Root { id: "N1" })<-[:ANY { value: 4 }]-(:Root { id: "N2" })-[:ANY { value: 2 }]->(:Root { id: "N3" }) RETURN nodes(path) as nodes',//5 | |
'MATCH path = (n1:Root { id: "N1" })<-[:ANY { value: 4 }]-(:Root { id: "N2" })-[:ANY { value: 2 }]->(n3:Root { id: "N3" }) WHERE (n1)-[:ANY { value: 3 }]->(n3) RETURN nodes(path) as nodes',//6 | |
'MATCH path = (:Root { id: "N1" })-[:ANY { value: 3 }]->(:Root { id: "N3" }) RETURN nodes(path) as nodes',//7 | |
'MATCH path = (:Root { id: "N1" })-[:ANY { value: 1 }]->(:Root { id: "N2" }) RETURN nodes(path) as nodes',//8 | |
'MATCH path = (:Root { id: "N1" }) RETURN nodes(path) as nodes',//9 | |
'RETURN NULL as nodes'//10 | |
] as patterns, lookAtPattern | |
CALL apoc.cypher.run(patterns[lookAtPattern], null) yield value // here we have all nodes of N1, N2 (red, orange, purple, green) | |
WITH [x IN value.nodes WHERE x.size > 1500 | x] as nodes // here DYNAMIC condition to filter out in my case by size > 1500 | |
RETURN nodes // now only (orange, purple, green) nodes left, red node excluded | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// CREATE (n1 {id:'N1', start:'1', end:'10'}), (n2 {id:'N2', start:'3', end:'7'}), (n3 {id:'N3', start:'5', end:'9'})
// MATCH (n1 {id:'N1'}), (n2 {id:'N2'})
// CREATE (n1) -[:ANY {start:'3', end:'4'}]-> (n2)
// MATCH (n1 {id:'N1'}), (n2 {id:'N3'})
// CREATE (n1) -[:ANY {start:'6', end:'9'}]-> (n2)
// MATCH (n1 {id:'N2'}), (n2 {id:'N1'})
// CREATE (n1) -[:ANY {start:'4', end:'7'}]-> (n2)
// MATCH (n1 {id:'N2'}), (n2 {id:'N3'})
// CREATE (n1) -[:ANY {start:'5', end:'7'}]-> (n2)
WITH '(n1) -[r]- (n2)' as body
CALL apoc.graph.fromCypher('MATCH path=' + body +' WHERE r.start <= $lookAt AND r.end > $lookAt RETURN *', { lookAt: '2' }, 'expl', {}) YIELD graph
WITH graph
RETURN *