Transcript from the "Complex Neo4j Queries" Lesson
>> Now the moment you've all been waiting for, degrees of Kevin Bacon, how far is an actor or an actress away from Kevin Bacon? There's a really cool thing that, Neo4j can do and it's called something called use shortest path. So it actually do a pathfinding algorithm on your graph database, and if you don't know how to do, pathfinding, there's another course in front of masters from this guy that talks about pathfinding algorithms.
[00:00:33] So you can actually take a course and figure out how it does that. But let's see how this works with Neo4j. So I wanna find out what's the shortest path between any actor. So in this case, let's just do Keane Reeves. How many degrees away from Kevin Bacon is Keane Reeves, so I'm gonna say match.
[00:00:54] We'll call this a path of some variety. And that's gonna be equal to shortest path. It's very aptly named thing. Okay, and then I'll put here we'll have a Bacon, as Lord Bacon, as I like to sometimes call him, person. And name is Kevin Bacon. Okay, and then here we're gonna describe a relationship and here we're gonna say any relationship.
[00:01:30] We don't care what the relationship is so I'm gonna say star so that's gonna include directed followed rote we're just looking for any connection between two people. If we want to only look in acted in, then here we would put acted in. But in this particular case, we can't we don't care.
[00:01:45] We want to see all relationships possible between Kevin Bacon and Keane Reeves. So we'll say, actually we don't even want an arrow there because both of them are gonna have directional relationships. So if I leave this angle bracket out. It'll actually just query for any directional relationship. He won't look at the direction of the relationship, which is what we want.
[00:02:13] We'll put Keanu, who's a person and the name is Keanu Reeves. Okay, and then underneath that, we were just gonna say return paths like that. Yeah. If you had a massive graph database, you this could be a very potentially expensive query. We don't care so we're just gonna ignore this little warning right there.
[00:02:54] So if I click play here, then we're gonna see the shortest path between Kevin Bacon and canneries which is gonna go through something's got to give to Jack Nicholson to a few good men to Kevin Bacon. Pretty cool, right? We now have like a database that we can query to figure out connections between actors and actresses.
[00:03:20] Let's say like we wanted to do, Porsche just named this like p or something like that. And so instead of we could do here. Charlize Theron, I think that's how you spell her name. There you go. Charlize Theron was in That Thing You Do with Tom Hanks who was in Apollo 13 with Kevin Bacon.
[00:03:49] So there's a bunch of different these kind of like functions to find paths between people a path is a data type in. Neo4j that allows you to look at kinda paths around your database like you can imagine. Let's say you're on LinkedIn, for example. And you wanna find what's the shortest path between me and the I don't know CEO of Salesforce or something like that, right?
[00:04:13] You could do this kinda same thing to say like, Well, I know this person, this person worked with this person and this person did a project with this other person and that's how we're connected, right? It doesn't even have to be people, right? Like one of the examples here in this database is logistics, right?
[00:04:32] So what's the easiest way for me to obtain milk, right? You can say like, well, I can work with this distributor that is connected to this dairy farm you can use kind of, you can derive those kind of insights as well from graphs. So graphs are really cool.
[00:04:47] They can describe lots of things that are not just movies, but it's a pretty concrete way for you to imagine that. So just to kinda show you how you would do this kind of in cipher shell without this neat visualization. Cause again, if you ran this query, for example, if you ran this in cipher shell here, this is gonna be, you're gonna get all these.
[00:05:08] Things about this is kind of unreadable. So what you could do here, if I put length path here like this. This will tell me at least how length path would be 4. You have to take 4 hops to get there. Let's another good way for us to visualize in cipher shells where we don't have these kind of fun visualizations is we can use something called unwind.
[00:05:39] So I'm gonna show you how to do that really quick. So we have this path this path thing is like it's a big data structure. And we wanna have a more useful way we can actually just see listed out all the titles names of these connections. So what you can do, instead of that is we can say, unwind nodes of path.
[00:06:01] So when we say nodes, there's just no nodes, okay? We're gonna unwind the nodes of the path. So we're gonna ignore the relationships for now, as node and this is basically, essentially like kind of a for loop for Neo4j. So now whatever it gets run underneath, this is going to be run multiple time.
[00:06:28] So this is gonna get run for each node in our particular path. So I'm gonna say return. And I wanna return names and titles, right? Because some things have names and some things have titles. People have names, movies have titles, so I need to return both of them.
[00:06:44] So there's a cool thing called Coalesce. And I can say node.name or node.title, as, I don't know name or title. Or we can even do this way. Yeah, name or title. I will do like that. Okay, so now what this will do is it is going to coalesce is gonna say, if this exists, I am gonna return that.
[00:07:21] If not, then I am gonna go to the second thing. And the second thing will be title. So if I click play here you can see it now it just returns Kevin Bacon, Apollo 13, Tom Hanks, That Thing You Do, Charlize Theron, right? So it kind of unwinds that so that we can kind of pull the individual pieces out of these particular elements that we want to.