Neo4j cypher query from Training -
i finished training @ http://www.neo4j.org/learn/online_course , had couple of questions lab answers.
the first advanced graph lab in lesson 2. (no answer given , doesn't verify in graph widget thingie)
the question is: recommend 3 actors keanu reeves should work (but hasn’t). hint should pick 3 people have acted_in relationships movies keanu hasn't acted_in.
the graph has person nodes , movie nodes acted_in relationships , directed relationships.
i came this:
match (a:person)-[:acted_in]->(movie:movie) not (:person {name:"keanu reeves"})-[:acted_in]->(movie) return a, count(movie) order count(movie) desc limit 3
but couldn't tell if excluded same movie or keanu reeves (because actors returned hadn't been in keanu's movies, might have been returned anyway.
i've found 2 solutions far.
1: recommend busiest actors keanu reeves has not acted with.
match (p:person)-[:acted_in]->(m) p.name <> 'keanu reeves' , not (p)-[:acted_in]->()<-[:acted_in]-(:person{name:'keanu reeves'}) return p.name, count(m) rating order count(m) desc limit 3;
which yields
p.name | rating -------------------------- tom hanks | 12 meg ryan | 5 cuba gooding jr.| 4
2: recommend actors keanu reeves' co-stars have collaborated most
match (f:person)-[:acted_in]->(m)<-[:acted_in]-(c:person), (k:person{name:'keanu reeves'}) c.name <> 'keanu reeves' , (f)-[:acted_in]->()<-[:acted_in]-(k) , not (c)-[:acted_in]->()<-[:acted_in]-(k) return c.name, count(c) rating order rating desc limit 3;
which yields
p.name | rating -------------------------- danny devito | 2 j.t. walsh | 2 tom hanks | 2
Comments
Post a Comment