Transcript from the "Neo4j Browser" Lesson
>> So one of the nice things that Neo4j kind of provides for you out of the box is it actually has a very nice browser experience. And I should say that in the Postgres section, we could have gone over pgAdmin. That's a very compatible project with Postgres. We can have like a nice browser experience.
[00:00:17] It's like phpMyAdmin if you're familiar with MySQL. I think MongoDB has an open source project similar as well. The nice thing about Neo4j is it's actually built directly into Neo4j. So I want you to just open your browser and I want you to go to localhost:7474 like that, and you should end up in this Neo4j browser.
[00:00:41] We set no authentication. So you should be able to just say connect and it should just work. And you get this really cool kind of database explorer. The thing I wanna call it here at the top, this is actually a query section. So if I say, MATCH (n) RETURN n; like that, it'll actually run this query here, and it gives us a nice little visual explorer here.
[00:01:14] What is this one? This is our little sad Michael Cera that I wasn't able to connect to this. So this actually returned to us our entire graph here that we've created with Scott Pilgrim vs the World, and it's kinda fun and you can move it around. You can click on it and expand or hide the child relationships.
[00:01:36] There's a bunch of cool stuff like that you can do with this. Anyway, dismiss it, that's fun. You can also get this in a table, right? So this is the table layout of what we returned here, or text or code. There's a bunch of stuff you can do, various different ways you can look at this.
[00:01:53] But we'll be using this little box here at the top. It's kinda hard to see, that's why I call it out. But all of your queries will run here and then it just adds the new kind of card here and it pushes the rest down. Here on the side, you can kinda get like a database explorer.
[00:02:09] It'll tell you that we have nine nodes in our database, we have seven relationships. We have movies and persons, we have acted in and directed relationships. And here's some also properties. So if I click on this, it'll show me all of the nodes with names in them. It's a nice little way to kind of explore your database.
[00:02:33] Okay, so one of the cool things about this is it actually has a bunch of different sample databases and we're gonna use one of these. So I'm gonna say, :play movies, and it's gonna drop down this little kind of tutorial thing. So do that, and this is fun to follow.
[00:02:51] You can do this as well. If you click over here, it'll have this big query here. I want you to click on the query here and it's gonna populate all that up here. This is a very long query that's going to create a lot of movies and actors and actresses in our database.
[00:03:09] So if I do that, and then I click play here, it's going to return to us here. Here's all the movies that Tom Hanks, for example, has starred in, as well as people that have acted with Tom Hanks and directed and these various different capacities. But that's not it.
[00:03:33] Now, we have like 130 some actors in our database, which is cool, actors and actresses. Cool, is everyone with me so far? Okay, if you're having a problem with that and you wanna keep using cypher-shell, I actually did here. If you come into Neo4j browser, actually kind of the outline of how you do it cuz it's kind of a pain to get this to work.
[00:04:07] Yeah, here's a link right here. If you click this link down here at the bottom of the course website, it'll give you just that whole queries that you can copy and paste into cypher-shell, and everything will just work just fine. Okay, so now, we have a movie database full of actors and actresses and directors, and we can start querying that with fun different kind of queries.
[00:04:31] So let's keep doing this in the browser here. You can do these queries either in the browser or in the cypher-shell, both of those work just fine for me. The cypher-shell just tends to be a little bit prettier. Or sorry, the other way around, the browser ends up being a little bit prettier So let's do that.
[00:04:50] So I wanna see all of the labels that I have in my new movie database. So I'm gonna to say, MATCH (n), so match all of the nodes in my particular database, and I'm gonna say RETURN DISTINCT labels(n), and count (*). Actually, you probably wanna, anyway. So what this is gonna do is it's gonna go and match every node in our database.
[00:05:24] And then distinct, you probably remember from Postgres, is going to only return. It's gonna deduplicate all of this. It's gonna grab all of the labels from all of these nodes, and then it's going to count them up for us. It's going to aggregate them is the term for that.
[00:05:38] So if we run that, we can see one of them doesn't have a label, which is why that Michael Cera one wasn't working. There we go, we can actually go fix that. We have 140 that have person and we have 39 that have movie associated with it. So that's good.
[00:05:56] Let's go see in all the various different kinds of relationships that we have. So we can say, MATCH (n)-[r], and then we're not gonna look at anything here. You could put a variable here, but we don't care what it's actually matching to. So I'm just gonna leave that empty.
[00:06:16] And I'm going to RETURN type(r), and then again, count(*) to aggregate all that. And we can see here we have directed, acted in, produced, wrote, reviewed, follows. So there's a bunch of different kind of relationships that we have here as well. Okay, so let's just pick an actor and actress and start making some queries to kinda figure out what kind of things that they've acted in with other people.
[00:06:46] We were looking at Aubrey Plaza before, but let's pick another actor that has more relationships. So I'm gonna pick Keanu Reeves just cuz I also like Keanu Reeves. So I'm gonna say, MATCH (Keanu:Person) and I'm gonna say, [:Acted_IN], In (m:Movie), With other people who [:ACTED_IN], and we'll just call this person Costar, which will be another person.
[00:07:28] Oops, how do we get Shift+Enter? Okay, so if you hold Shift and hit Enter, it'll allow you to do multiple lines. WHERE Keanu's name is, in fact, Keanu Reeves. We got some nice autocompletion here. So I'm gonna say Keanu.name = "Keanu Reeves". RETURN DISTINCT Costar.name. It's because we're doing distinct, right?
[00:08:03] Because Keanu Reeves has been in, for example, Keanu Reeves is in all three Matrices. [LAUGH] Keanu Reeves is in all three Matrix films, let's go with that. So that means that, for example, Carrie-Anne Moss was in three different distinct films. So let's find out who was in the most films with Keanu Reeves.
[00:08:26] So we'll do a count(*) here as well, And then we'll ORDER BY, Count(*) DESCENDING. And then after that, let's alphabetize. So then, you'll say Costar.name. So if we do this, now, you have to click play up here, Dismiss this little thing here as well. We're gonna get Carrie-Anne Moss, Hugo Weaving, and Laurence Fishburne, no surprise, Matrix, who were in three different films with Keanu Reeves.
[00:09:08] And then a bunch of these other people who were just in one film with Keanu Reeves. Because we just returned Costar.name, it's just going to give us exactly what we asked for. But what's pretty cool here, instead of doing all of this aggregation, what we can do, I'm gonna just delete all this ORDER BY, and just RETURN Costar by itself and not DISTINCT.
[00:09:33] So to make this a little bit simpler, we're gonna return the entire node. And if we do that in the browser, the browser is like, cool, I can show you a visualization of this. So if I click play now, we're gonna get a nice little graph here. Now, we didn't return the relationships.
[00:09:50] If we return the relationships, that will actually make this even prettier for us. So if I click that and I say, RETURN, Let's say, we'll call this one a and return b, and we return Costar and a and b. Now, we get like a nice correlation. We should.
[00:10:19] We also need the movie as well, obviously, And m. Now, here we go. Now, you can see all these films that were in Keanu Reeves, they're disconnected. And so what's the central node that we're missing here? Keanu Reeves himself, right? So we also have to return Keanu. Now, after doing all of this, now we get a nice little graph.
[00:10:53] You can see all the stuff and how they are connected together in this kind of web of movies. So you can see these ones are like highly all attached to the Matrix films, The Replacements, Johnny Mnemonic, Something's Got to Give, and the Devil's Advocate. Now, if you're doing this in cypher-shell, obviously, it's far less impressive.
[00:11:15] In fact, if I just copy and paste this, it's gonna be really hard to read in cypher-shell cuz it's going to return to just a whole lot of no data, which is pretty hard to read on a command line like this. But in the web browser, it actually makes it pretty pleasant to look at.