Create a Neo4j property graph using Cypher

8 minute read

Property Graph

Neo4j allows you to create a graph database based on a property graph. In this post we will understand the core principles of a graph database (especially a property graph) and we will use Neo4j Desktop to create a property graph from scratch.

What is a property graph?:

A property graph is a type of graph. It has the classic structure of a graph, so nodes and arches, but it also provides the possibility to add properties to any entity inside the graph.

Neo4j is one of the most popular graph DBMS and it is built on the concept of property graph. The following section will explain the most important concepts you need to know before diving into the graph DB world.

  • NODE: A node is the most important element of a graph. It represents an entity you want to save and manage in your database. In a property graph, each node is labeled with a name that indicates the type of that node.

  • LABEL: A label is a string that is associated with the nodes in the graph. The label represents the entity of the node it’s associated with. Multiple nodes can have the same label and a node can have multiple labels attached.

  • RELATIONSHIP: A relationship is represented as the arch that connects two nodes. A relationship indicates that there is a bond between two nodes (it can be also defined between the same node). The arch that represents a relationship needs to be oriented.

  • RELATIONSHIP TYPE: Each relationship inside a property graph needs to have a relationship type. It has the same function as per the label for the node: it identifies which is the type of relationship between two nodes (example: node A has a relationship with node B, and the relationship type is: is_father_of). The way of the arch between the two nodes identifies which node has the role explained in the relationship type.

  • PROPERTY: A property is a key-value entity that can be associated both to nodes and arches. A property, as the name suggests, represents a property that has a name and a value (example: the node with the label Actor has the property “name”: “Clint Eastwood”).

Cypher

Neo4j uses a very intuitive and clever language to create and query data: Cypher. Cypher is a language that uses the ASCII-Art syntax. This is a graphic representation of what you want to explain. Following this convention, if you want to refer to a node in the graph, you use round brackets(); if you want to refer to a relationship, you use square brackets []. An example of a Cypher syntax is this: (a)-[:TEACHES]->(b) (it means that a teaches b).

Next we will use Cypher to create nodes and relationships to populate a Neo4j graph database.

Example: Create your first graph database using Cypher

Launch Neo4j Desktop application and start a database see this post

Use Cypher to create nodes (entities) and arches (relationships) in a graph database. We will create three different nodes and two relationships. The three nodes will be labeled as User, Post, and Image.

The label identifies the type of the node.

We will then create two relationships between nodes. The first one will be between the User and Post, and will have a type of LIKE. The second one will be between the User and Image, and will have a type of TOOK.

Move to the upper part of the Neo4j browser application, and find the query bar where you will enter all your Cypher commands:

CREATE (a:User {name: "Shravan Kuchkula", age: "36", country: "USA"})
RETURN a;

By prompting this command, you will create a User node with three properties: name, age, and country. Because you put the RETURN statement, this new node will be returned in a graphic form:

Note that if you click on it, the label and properties of the node are shown in the bottom part of the graph.

Enter the following Cypher code to create the Post node, and click the Play button:

CREATE (p:Post {title: "Daily Stoic", date: "10-05-2020", author: "Seneca"})
RETURN p;

By prompting this command, you will create a Post node with three properties: title, date, and author.

Enter the following Cypher code to create the Image node, and click the Play button:

CREATE (i:Image {place: "Italy"})
RETURN i;

By prompting this command, you will create a Image node with one property: place.

Now that the entities have been created, you need to create the relationships between them.

Enter the following Cypher code to create a LIKE relationship between the User and the Post:

MATCH (a:User), (p:Post)
WHERE a.name = "Shravan Kuchkula" AND p.title = "Daily Stoic"
CREATE (a)-[r:LIKE {day: "Monday"}]->(p)
RETURN r;

By prompting this command, you will create a relationship between the User node that has the name property set to Shravan Kuchkula, and a Post node that has the title property set to Daily Stoic. This query is required because you could have more than one node with the same label. The relation has a property itself, and it represents the day of the week the photo has been taken.

Enter the following Cypher code to create a TOOK relationship between the User and the Image:

MATCH (a:User), (i:Image)
WHERE a.name = "Shravan Kuchkula" AND i.place = "Italy"
CREATE (a)-[r:TOOK]->(i)
RETURN r;

By prompting this command, you will create a relationship between the User node that has the name property set to Shravan Kuchkula, and a Image node that has the place property set to Italy.

It’s time to see the graph you create. Enter the following Cypher code to retrieve it:

MATCH (a:User)-[r]->(b)
WHERE a.name = "Shravan Kuchkula"
RETURN a, r, b;

By promtping this command, you’re asking Neo4j to retrieve all the nodes whose label is User and that have a name property set to Shravan Kuchkula. You are also asking Neo4j to retrieve the relationships this node is involved in and the target nodes of the relationship. The result graph is that:

Appendix

More on Cypher Query language

Cypher is a graph query language

It will help you start thinking about graphs and patterns, apply this knowledge to simple problems, and learn how to write Cypher statements.

Neo4j’s Property Graphs are composed of nodes and relationships, either of which may have properties. Nodes represent entities, for example concepts, events, places and things. Relationships connect pairs of nodes.

Concept of patterns: Nodes and relationships can be considered as low-level building blocks. The real strength of the property graph lies in its ability to encode patterns of connected nodes and relationships. A single node or relationship typically encodes very little information, but a pattern of nodes and relationships can encode arbitrarily complex ideas.

Cypher, Neo4j’s query language, is strongly based on patterns.

Specifically, patterns are used to match desired graph structures. Once a matching structure has been found or created, Neo4j can use it for further processing.

A simple pattern, which has only a single relationship, connects a pair of nodes (or, occasionally, a node to itself). For example, a Person LIVES_IN a City or a City is PART_OF a Country.

Complex patterns, using multiple relationships, can express arbitrarily complex concepts and support a variety of interesting use cases. For example, we might want to match instances where a Person LIVES_IN a Country. The following Cypher code combines two simple patterns into a slightly more complex pattern which performs this match:

(:Person) -[:LIVES_IN]-> (:City) -[:PART_OF]-> (:Country)

1. Node Syntax

Cypher uses a pair of parentheses to represent a node: () .Below are some examples of nodes, providing varying types and amounts of detail:

()
(matrix)
(:Movie)
(matrix:Movie)
(matrix:Movie {title: "The Matrix"})
(matrix:Movie {title: "The Matrix", released: 1997})

The simplest form, (), represents an anonymous, uncharacterized node. If we want to refer to the node elsewhere, we can add a variable, for example: (matrix). A variable is restricted to a single statement. It may have different or no meaning in another statement.

The :Movie pattern declares a label of the node. This allows us to restricts the pattern, keeping it from matching (say) a structure with an Actor node in this position.

The node’s properties, for example title, are represented as a list of key/value pairs, enclosed within a pair of braces, for example: {name: "Keanu Reeves"}. Properties can be used to store information and/or restrict patterns.

2. Relationship syntax

Cypher uses a pair of dashes (--) to represent an undirected relationship. Directed relationships have an arrowhead at one end (<--, -->). Bracketed expressions ([…]) can be used to add details. This may include variables, properties, and type information:

-->
-[role]->
-[:ACTED_IN]->
-[role:ACTED_IN]->
-[role:ACTED_IN {roles: ["Neo"]}]->

3. Pattern syntax

Combining the syntax for nodes and relationships, we can express patterns. The following could be a simple pattern (or fact) in this domain:

(keanu:Person:Actor {name:  "Keanu Reeves"} )
-[role:ACTED_IN     {roles: ["Neo"] } ]->
(matrix:Movie       {title: "The Matrix"} )

Equivalent to node labels, the :ACTED_IN pattern declares the relationship type of the relationship. Variables (eg, role) can be used elsewhere in the statement to refer to the relationship.

As with node properties, relationship properties are represented as a list of key/value pairs enclosed within a pair of braces, for example: {roles: ["Neo"]}. In this case, we used an array property for the roles, allowing multiple roles to be specified. Properties can be used to store information and/or restrict patterns.

4. Pattern variables

To increase modularity and reduce repetition, Cypher allows patterns to be assigned to variables. This allows the matching paths to be inspected, used in other expressions, etc.

acted_in = (:Person)-[:ACTED_IN]->(:Movie)

The acted_in variable would contain two nodes and the connecting relationship for each path that was found or created. There are a number of functions to access details of a path, for example: nodes(path), relationships(path) and length(path).

5. Clauses

Cypher statements typically have multiple clauses, each of which performs a specific task, for example:

  • create and match patterns in the graph
  • filter, project, sort, or paginate results
  • compose partial statements

By combining Cypher clauses, we can compose more complex statements that express what we want to know or create.

Tags:

Updated: