# Week 8 - Neo4j

This week's lab focuses on setting up and using Neo4j.

* How to install and set up Neo4j
* Using the Neo4j interface - Running a Neo4j demo - Running queries - Exploring a graph with the mouse - Changing node/relationship colours and labels - Understanding the "Connect result nodes" option
* Planning a graph using Arrow Tool and importing it into Neo4j

## 1. Setting up a Neo4j database <a href="#id-1.-setting-up-a-neo4j-database" id="id-1.-setting-up-a-neo4j-database"></a>

You have two options to install Neo4j on your device: local setup (Section 1.1) or cloud setup (Section 1.2).

### **1.1 Local setup (recommended)**

It is strongly recommended that you set up Neo4j locally if possible.

* Head to [https://neo4j.com/download/](https://neo4j.com/download/) to download the Neo4j desktop.
* Install it the same way as you would install any other program.
* After installing, open Neo4j Desktop.
* Create a new "Project" by going to Projects, then clicking "Add" at the top right, and "Local DBMS". Give it a name and a password.
* Once it has been created, find the newly created database in the menu and click Start. Once started, click Open and this will open your database in the Neo4j browser.

### 1.2 Neo4j Aura

If you cannot set up Neo4j locally, you can try Neo4j Aura, a cloud-based version of Neo4j. To set up a graph:

* Head over to [https://neo4j.com/cloud/aura/](https://neo4j.com/cloud/aura/) and set up a free account.
* Once your email is validated, an instance will be created for you.

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2FjCTvBJXXKH4l9zA2LShw%2Fimage.png?alt=media&#x26;token=fb83bcef-4c8e-4ca8-9b16-296a0215e356" alt=""><figcaption></figcaption></figure>

* In order to use the instance, click on "Open". You will be asked to type your password. You can find it in the .txt file you downloaded just now.

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2FVaLI3QNCga77mjTQYIqU%2Fimage.png?alt=media&#x26;token=4142dc87-b331-4fd9-a6c6-350c7651c69b" alt=""><figcaption></figcaption></figure>

* Make sure to copy the credentials and save them somewhere safe.
* It will take a few minutes for your database to be set up.
* Once set up, click on the "Neo4j Guides" button on the database. Then you can explore Neo4j.

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2F7LdprlV6wNc7YhdgBltB%2Fimage.png?alt=media&#x26;token=4630e7dd-2c3e-4d0e-9e5a-c3bc3f05d3e7" alt=""><figcaption></figcaption></figure>

## 2. Neo4j browser interface <a href="#id-2.-neo4j-browser-interface" id="id-2.-neo4j-browser-interface"></a>

Your first task is to familiarise yourself with the Neo4j browser interface.

In the vertical menu on the left, have a look through each of the submenus - Database, Favourites, Project Files, etc.

Take a look at Guides in particular. Check out the intro guide by clicking on the `:guide intro` - it provides a nice overview of the core components of the Neo4j browser. Feel free to read through each of the other guides in the menu.

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2FjzhA9mxmu8xW2zjrwxaa%2Fimage.png?alt=media&#x26;token=718751cf-591d-4076-8878-c02f7ef4c1c8" alt=""><figcaption></figcaption></figure>

Finally, take a look at the options menu (at the bottom left), and see if you can figure out what each of the settings does. Take particular note of the "connect result nodes" checkbox - we will come back to this later.

## 3. Intro to Neo4j graphs - Movies Demo <a href="#id-3.-intro-to-neo4j-graphs-movies-demo" id="id-3.-intro-to-neo4j-graphs-movies-demo"></a>

Neo4j provides a few nice demo graphs to play around with and familiarise yourself with graph databases. You can start by trying the Movies demo, which is a database containing actors and directors.

In the **query box**, type the following:

&#x20;   `:play movies`

and press ctrl+enter (or click the blue arrow on the right). You'll be greeted with the following:

![](https://679195194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0dbR2mMn3b448npr0oe8%2Fuploads%2FhbSUemurQt2FpUiJ8XpQ%2Fimage.png?alt=media\&token=541a72ad-4d08-44f2-8e46-dc45d5332d1f)

If you can't connect to Neo4j, go to DBMSs and start the _Movie DBMS._ Then click on Open. A new neo4j browser window will be created, and then you can view the Movies demo.

In the **query box**, type the following:

&#x20;   `:play movies`

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2FoPiq9PyvY8eF9uaHXbR4%2Fimage.png?alt=media&#x26;token=9c324f42-d7b6-4fa1-8e10-2c3b2756e234" alt=""><figcaption></figcaption></figure>

Navigate through the Movies demo using the arrows at the bottom. You can run the query displayed in the grey box by clicking on it (which automatically places it into the query box), then running it via ctrl+enter or clicking the blue arrow\*.

After you run the query you might like to try navigating around the graph using the mouse. Have a go at expanding/minimising nodes and moving nodes around.

See if you can figure out how to change the colour of the nodes and the width of the relationships. You can also try changing the text displayed on the node - for example, changing the Person nodes to display birth years instead of names.

Now see if you can find all of the movies Tom Hanks starred in just by looking at the graph. Do you think this graph shows all movies and actors in the database? Why/why not?

Now run each of the queries on the remaining pages. We won't go into detail on how the Cypher queries work just yet, so just try to focus on understanding whether the queries work as you'd expect based on the descriptions of the queries in the demo.

Finally, let's run a simple query of our own:

```cypher
MATCH (n)
RETURN n
```

This query will show every single node in the graph. You should see that the nodes are connected, even though we haven't queried for relationships at all. Now, try going into the options menu (on the bottom left), and deselecting the **Connect result nodes** button. Run the query again - what happens?

**\*Note that if you accidentally run the CREATE statements twice,** you'll end up with duplicate nodes/edges. I'd recommend deleting the entire graph and starting over. You can do this by running this query:

```cypher
MATCH (n)
DETACH DELETE (n)
```

This will delete all **nodes** and **edges** from the graph.

## 4. Using the Arrow Tool

The last exercise for this week is to familiarise yourself with the **Arrow Tool**, a simple web application for designing graph database schemas.

You can access the Arrow Tool via [https://arrows.app/](https://arrows.app/).&#x20;

Your task for this week is to create a simple Movies-based graph that captures the following information:

* Robert De Niro acted in the movies "Casino", and "Goodfellas".
* Joe Pesci acted in the movies "Goodfellas", "Casino" and "Home Alone".
* McCaulay Culkin acted in the movie "Home Alone".

See if you can create this simple graph using the Arrow Tool. Make sure that your "captions" are the node labels (i.e. "Movie" and "Person") and that your properties are the names or titles, for example:

`name: Joe Pesci`&#x20;

The relationships should have the caption "ACTED\_IN".

Then, click the "Export Cypher" button and paste this into your Neo4j browser. Make sure you have deleted your old movies database first (with the detach delete query above, which is also on page 7 of the demo).

Now run the `MATCH (n) RETURN (n)` query again (make sure **Connect Result Nodes** is enabled in the options). You should see something like the following:

![](https://679195194-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0dbR2mMn3b448npr0oe8%2Fuploads%2FSmSZ85OY8ovXoy0ue7nZ%2Fimage.png?alt=media\&token=0baa09fc-f224-494c-afa6-3bc7ffe9e772)

## 5. Change Colours for Nodes

You can change the colour for nodes in neo4j.&#x20;

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2FrvakK0EcSqcZD9aC2k2P%2F1736afb63aa5090f4bad5e380d8bbe7.png?alt=media&#x26;token=45e713ef-bf81-4069-ae7a-802a556624e4" alt=""><figcaption></figcaption></figure>

For example, there are 4 colours for 4 different node types. If you want to change the colour for Column nodes, just need to click on "Column (11)" and then select a colour you prefer. The size and caption of a node also can be changed in this way.

<figure><img src="https://374096590-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FE6tM8okJTaOtct7O9mvr%2Fuploads%2FtEAsIvJIjFe0O4Hhdr66%2Fimage.png?alt=media&#x26;token=37c31957-d7cf-432c-93ae-43e342558d0d" alt=""><figcaption></figcaption></figure>
