Prev |  | Next

## 4.4. Finding Paths

Our example graph consists of movies with title and year and actors with a name. Actors have `ACTS_IN` relationships to movies, which represents the role they played. This relationship also has a role attribute.

We queried and updated the data so far, now let’s find interesting constellations, a.k.a. paths.

    CREATE (matrix1:Movie { title : 'The Matrix', year : '1999-03-31' })
    CREATE (matrix2:Movie { title : 'The Matrix Reloaded', year : '2003-05-07' })
    CREATE (matrix3:Movie { title : 'The Matrix Revolutions', year : '2003-10-27' })
    CREATE (keanu:Actor { name:'Keanu Reeves' })
    CREATE (laurence:Actor { name:'Laurence Fishburne' })
    CREATE (carrieanne:Actor { name:'Carrie-Anne Moss' })
    CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix1)
    CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix2)
    CREATE (keanu)-[:ACTS_IN { role : 'Neo' }]->(matrix3)
    CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix1)
    CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix2)
    CREATE (laurence)-[:ACTS_IN { role : 'Morpheus' }]->(matrix3)
    CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix1)
    CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix2)
    CREATE (carrieanne)-[:ACTS_IN { role : 'Trinity' }]->(matrix3)

All other movies that actors in “The Matrix” acted in ordered by occurrence:

    MATCH (:Movie { title: "The Matrix" })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)
    RETURN movie.title, count(*)
    ORDER BY count(*) DESC ;

movie.title | count(*)
--- | ---
2 rows
`"The Matrix Revolutions"` | `3`
`"The Matrix Reloaded"` | `3`

Let’s see who acted in each of these movies:

    MATCH (:Movie { title: "The Matrix" })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)
    RETURN movie.title, collect(actor.name), count(*) AS count
    ORDER BY count DESC ;

movie.title | collect(actor.name) | count
--- | --- | ---
2 rows
`"The Matrix Revolutions"` | `["Keanu Reeves","Laurence Fishburne","Carrie-Anne Moss"]` | `3`
`"The Matrix Reloaded"` | `["Keanu Reeves","Laurence Fishburne","Carrie-Anne Moss"]` | `3`

What about co-acting, that is actors that acted together:

    MATCH (:Movie { title: "The Matrix"
      })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)<-[:ACTS_IN]-(colleague)
    RETURN actor.name, collect(DISTINCT colleague.name);

actor.name | collect(distinct colleague.name)
--- | ---
3 rows
`"Carrie-Anne Moss"` | `["Keanu Reeves","Laurence Fishburne"]`
`"Laurence Fishburne"` | `["Keanu Reeves","Carrie-Anne Moss"]`
`"Keanu Reeves"` | `["Laurence Fishburne","Carrie-Anne Moss"]`

Who of those other actors acted most often with anyone from the matrix cast?

    MATCH (:Movie { title: "The Matrix"
      })<-[:ACTS_IN]-(actor)-[:ACTS_IN]->(movie)<-[:ACTS_IN]-(colleague)
    RETURN colleague.name, count(*)
    ORDER BY count(*) DESC LIMIT 10;

colleague.name | count(*)
--- | ---
3 rows
`"Carrie-Anne Moss"` | `4`
`"Keanu Reeves"` | `4`
`"Laurence Fishburne"` | `4`

Starting with paths, a path is a sequence of nodes and relationships from a start node to an end node.

We know that Trinity loves Neo, but how many paths exist between their actors? We’ll limit the path length and the query as it exhaustively searches the graph otherwise

    MATCH p =(:Actor { name: "Keanu Reeves" })-[:ACTS_IN*0..5]-(:Actor { name: "Carrie-Anne Moss" })
    RETURN p, length(p)
    LIMIT 10;

p | length(p)
--- | ---
9 rows
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[0]{role:"Neo"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[3]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[4]{role:"Morpheus"},Node[1]{title:"The Matrix Reloaded",year:"2003-05-07"},:ACTS_IN[7]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `4`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[0]{role:"Neo"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[3]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[5]{role:"Morpheus"},Node[2]{title:"The Matrix Revolutions",year:"2003-10-27"},:ACTS_IN[8]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `4`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[0]{role:"Neo"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[6]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `2`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[1]{role:"Neo"},Node[1]{title:"The Matrix Reloaded",year:"2003-05-07"},:ACTS_IN[4]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[3]{role:"Morpheus"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[6]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `4`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[1]{role:"Neo"},Node[1]{title:"The Matrix Reloaded",year:"2003-05-07"},:ACTS_IN[4]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[5]{role:"Morpheus"},Node[2]{title:"The Matrix Revolutions",year:"2003-10-27"},:ACTS_IN[8]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `4`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[1]{role:"Neo"},Node[1]{title:"The Matrix Reloaded",year:"2003-05-07"},:ACTS_IN[7]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `2`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[2]{role:"Neo"},Node[2]{title:"The Matrix Revolutions",year:"2003-10-27"},:ACTS_IN[5]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[3]{role:"Morpheus"},Node[0]{title:"The Matrix",year:"1999-03-31"},:ACTS_IN[6]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `4`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[2]{role:"Neo"},Node[2]{title:"The Matrix Revolutions",year:"2003-10-27"},:ACTS_IN[5]{role:"Morpheus"},Node[4]{name:"Laurence Fishburne"},:ACTS_IN[4]{role:"Morpheus"},Node[1]{title:"The Matrix Reloaded",year:"2003-05-07"},:ACTS_IN[7]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `4`
`[Node[3]{name:"Keanu Reeves"},:ACTS_IN[2]{role:"Neo"},Node[2]{title:"The Matrix Revolutions",year:"2003-10-27"},:ACTS_IN[8]{role:"Trinity"},Node[5]{name:"Carrie-Anne Moss"}]` | `2`

Bur that’s a lot of data, we just want to look at the names and titles of the nodes of the path.

    MATCH p =(:Actor { name: "Keanu Reeves" })-[:ACTS_IN*0..5]-(:Actor { name: "Carrie-Anne Moss" })
    RETURN extract(n IN nodes(p)| coalesce(n.title,n.name)) AS `names AND titles`, length(p)
    ORDER BY length(p)
    LIMIT 10;

names and titles | length(p)
--- | ---
9 rows
`["Keanu Reeves","The Matrix","Carrie-Anne Moss"]` | `2`
`["Keanu Reeves","The Matrix Reloaded","Carrie-Anne Moss"]` | `2`
`["Keanu Reeves","The Matrix Revolutions","Carrie-Anne Moss"]` | `2`
`["Keanu Reeves","The Matrix","Laurence Fishburne","The Matrix Reloaded","Carrie-Anne Moss"]` | `4`
`["Keanu Reeves","The Matrix","Laurence Fishburne","The Matrix Revolutions","Carrie-Anne Moss"]` | `4`
`["Keanu Reeves","The Matrix Reloaded","Laurence Fishburne","The Matrix","Carrie-Anne Moss"]` | `4`
`["Keanu Reeves","The Matrix Reloaded","Laurence Fishburne","The Matrix Revolutions","Carrie-Anne Moss"]` | `4`
`["Keanu Reeves","The Matrix Revolutions","Laurence Fishburne","The Matrix","Carrie-Anne Moss"]` | `4`
`["Keanu Reeves","The Matrix Revolutions","Laurence Fishburne","The Matrix Reloaded","Carrie-Anne Moss"]` | `4`

Copyright © 2014 Neo Technology

Prev | Up | Next
| Home |