Codementor Events

Cypher for Newbies Part 1 - CREATE and MATCH

Published May 07, 2015Last updated Feb 15, 2017

Before we start

This tutorial is based on Cypher 2.2, which runs on Neo4j Community 2.2.1.

Other versions of Neo4j may show some syntax differences.

Install Neo4j

Download and install Neo4J Community 2.2.1.

Start Neo4j

Run the Neo4j application, choose a folder and start serving the graph database.

At this point you should be able to open your web browser and access http://localhost:7474/browser/

The CREATE Statement

The CREATE statement creates a new node or relationship.

Let us start by creating a new node.

CREATE (s :Student {name: "John Doe", age: 34}) RETURN s;

CREATE is the Cypher statement. The information about the node is provided inside the parenthesis.

Let us get a closer look at each part.

The little s is a variable that we will use to reference this node at other parts of the Cypher query.

The :Student is a Label. We will learn more about Labels later.

For now, you should be aware that this node is being created with the Label Student.

A single node may have any number of Labels.

Inside the curly brackets {name: "John Doe", age: 34} we provide the node Properties.

This node have two properties, name and age.

Finally, we return the node we just created with RETURN s.

After running the Cypher Query, the Neo4j browser interface will show a node visualization. If you put the mouse over, you will be able to the node properties we just defined.

Now let us create another node.

CREATE (s :Student {name: "John Snow", age: 16}) RETURN s; 

At this point we should have two nodes, John Doe and John Snow.

The MATCH statement

The MATCH statement is used to find a node.

If we want to return the John Snow's node, we could use the following statement.

MATCH (s :Student {name: "John Snow"}) RETURN s;

MATCH is the query statement

s is just the node variable for this query.

:Student is the Label

{name: "John Snow"} is what we are looking for at the graph.

RETURN s tells Neo4j what we want to return, the full node we just found.

This Cypher query searches for Nodes WITH the label Student AND with a property name that equals "John Snow".

If we wanted to find Nodes with any Label and with the age that equals 16, we could use:

MATCH (a {age: 16}) return a;

That would also bring use the same node, John Snow.

So, if you can find a node by a property without the Label, why add the Label?

Let us take a look at the PROFILE statement.

You can add the PROFILE statement before any Cypher query.

Be careful, it profiles the query but also executes it's statements, so if you are profiling a CREATE query, you will CREATE new nodes or relationships.

Profile the first MATCH statement with:

PROFILE MATCH (s :Student {name: "John Snow"}) RETURN s;

You will see that to run this Cypher Query, Neo4j will perform a NodeByLabelScan.

It means that Neo4j will scan EVERY node with the Label :Student looking for the name John Snow.

Now, let us profile the other MATCH Cypher Query.

PROFILE MATCH (a {age: 16}) return a;

This will perform a AllNodesScan which means Neo4j has to scan EVERY node, not only the ones with the :Student label. This is, obviously, too expensive!

We now know how to create and find nodes. See you on Part 2 next week.

Discover and read more posts from Pablo Saraiva
get started
post commentsBe the first to share your opinion
Show more replies