Skip to content

Latest commit

 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Neo4j

Introduction to graph databases

When working with highly connected data, graph databases are ultra fast and much easier to query than relational databases. You can query key value pairs in a graph database and also query the relationships between entities. Querying relationships is a powerful tool because it helps you to find patterns in data and discover new insights.

Neo4j is the world’s leading graph database. It’s scalable, versatile and user-friendly. I used a Neo4j Aura workspace on my browser to create a graph representing a design team.

Design team graph

To recreate the graph in the design team.png file, run the following query against an empty Neo4j database:

CREATE
  (angele:Employee {name: 'Angele Kesta', salary:70000, location: 'Vilnius'}),
  (mantas:Employee {name: 'Mantas Pasta', salary:55000, location: 'Vilnius'}),
  (ronnie:Employee {name: 'Ronnie Scott', salary:110000, location: 'Tel Aviv'}),
  (justina:Employee {name:'Justina Detail', salary: 60000, location: 'Vilnius'}),
  (george:Employee {name: 'George Dzinga', salary: 80000, location: 'Amsterdam'}),
  (sally:Intern {university: 'Vilnius Technical University', name: 'Sally Jones', location: 'Vilnius'}),
  (events:Project {title: 'Events Seating Map'}),
  (angele)-[:DESIGNED {role: 'Lead Designer'}]->(events),
  (mantas)-[:DESIGNED {role: 'Junior Designer'}]->(events),
  (ronnie)-[:PROJECT_MANAGED {role: 'Head of Project'}]->(events),
  (justina)-[:EDITED]->(events),
  (george)-[:EDITED]->(events),
  (forums:Project {title: 'Forum Comments'}),
  (george)-[:DESIGNED {role: 'UX Writer'}]->(forums),
  (mantas)-[:DESIGNED {role: 'Junior Designer'}]->(forums),
  (ronnie)-[:MENTORED]->(angele),
  (ronnie)-[:MENTORED]->(mantas),
  (george)-[:MENTORED]->(mantas),
  (mantas)-[:MENTORED]->(sally)

This query is written in Cypher code which is Neo4j's graph query language, used to add nodes and properties to a graph, and to retrieve data.

Nodes

Nodes are used to represent real-world objects, such as people or products. In the design team graph, nodes represent employees, projects and an intern.

  • There are six nodes representing people in the design team. Five of these nodes have the label Employee and include properties about the employee’s name, salary and location. One of these nodes has the label Intern and includes properties about the intern’s university, name and location.

  • There are two project nodes with the label Project. The project titles are Events Seating Map and Forum Comments.

Relationships

The graph demonstrates the roles employees had in the projects and how employees interact with each other. This information is represented as relationships between nodes. A relationship describes a connection between a source node and a target node.

Let’s discuss Ronnie’s relationships in the graph to illustrate how relationships are structured in Neo4j and demonstrate the power of graph databases:

  • Cypher syntax is used to implement the mentor relationship between Ronnie Scott and Angele Kesta.

  • Cypher provides a visual way of demonstrating relationships, using a syntax similar to ASCII-art where (nodes)-[:ARE_CONNECTED_TO]→(otherNodes) using round brackets for circular (nodes), and -[:ARROWS]→ to represent relationships.

  • The Cypher syntax representing the design team graph shows that (ronnie)-[:MENTORED]→(angele).

  • Ronnie Scott also mentored Mantas Pasta.

  • Ronnie Scott was also Head of Project while designing the Events Seating Map. Therefore the relationship between Ronnie and the Events Seating Map is labeled PROJECT_MANAGED and includes a property role with the value Head of Project.

Note

There are additional relationships in the graph, as the employees mentored each other, and one employee mentored Sally the intern. The employees also have relationships with the projects. Refer to the design team.png file to see which employees designed or edited the projects and their exact roles. You will see all the properties of the nodes, and the properties of the relationships.

Conclusion

If I had a larger graph modelling an entire organisation, a fantastic route for exploration would be to see who has impacted the most employees in terms of mentoring (direct mentees, mentees of direct mentees etc.) and how the projects benefitted from these relationships.

Cypher can be used to run queries to get answers about nodes and their relationships. This section provides example queries and information retrieved from the graph.

Count the number of mentees a mentor has

Query
MATCH (mentor:Employee|Intern)-[:MENTORED]->(mentee:Employee|Intern)
RETURN mentor.name, COUNT(*) AS num_mentees
ORDER BY num_mentees DESC

This query returns the name of the employee or intern who has the most mentees first, in this case it’s Ronnie Scott, then orders the rest of the results in descending order.

Table 1. Result
mentor.name num.mentees

"Ronnie Scott"

2

"George Dzinga"

1

"Mantas Pasta"

1

Rows: 3

Indirect mentees

Query
MATCH (ronnie:Employee {name: 'Ronnie Scott'})-[:MENTORED]->(mentees)-[:MENTORED]->(mentees_of_mentees)
RETURN mentees_of_mentees.name

Ronnie Scott’s indirect mentee is Sally Jones. This query works by first matching Ronnie Scott, his direct mentees, then all of the mentees of his direct mentees.

Table 2. Result
mentees_of_mentees.name

"Sally Jones"

Rows: 1

Returning a node

Query
MATCH(e:Employee {salary:70000})
RETURN e

Returns the employee who earns a salary of 70000 and the properties.

Table 3. Result
e

(:Employee {name: "Angele Kesta", location: "Vilnius", salary: 70000})

Rows: 1

Using a minimum

Query
WITH 60000 AS minSalary
MATCH (a:Employee WHERE a.name = 'Ronnie Scott')-[:MENTORED]->(b:Employee WHERE b.salary > minSalary)
RETURN b.name

Returns the employees who were mentored by Ronnie Scott and have a salary greater than 60000.

Table 4. Result
b.name

"Angele Kesta"

Rows: 1

Finding a project and its editors

Query
MATCH (angele {name: 'Angele Kesta'})-[:DESIGNED]->(project)<-[:EDITED]-(editor)
RETURN project.title, editor.name

Returns the project Angele Kesta designed and its editors.

Table 5. Result
project.title editor.name

"Events Seating Map"

"Justina Detail"

"Events Seating Map"

"George Dzinga"

Rows: 2

Finding who designed and edited Events Seating Map

Query
MATCH (events {title: 'Events Seating Map'})<-[:DESIGNED|EDITED]-(employee)
RETURN employee.name

Returns nodes with a DESIGNED or EDITED relationship towards the project node Events Seating Map.

Table 6. Result
employee.name

"Angele Kesta"

"Mantas Pasta"

"Justina Detail"

"George Dzinga"

Rows: 4

Using true/false

Query
MATCH (e:Employee {name: 'Ronnie Scott'})
RETURN e.salary < 99000

This checks if the salary of the employee Ronnie Scott is less than 99000, and returns false as Ronnie earns 110000.

Table 7. Result
e.salary < 99000

false

Rows: 1

About

A diagram of a team and their projects, which has been turned into a graph using the Neo4j database management system. The README includes a discussion about the graph's structure and includes example Cypher queries.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors