Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
136 lines (85 sloc) 4.33 KB

Organisation Graph example queries

The syntax for querying a graph database can be unfamiliar and quite difficult so in this page I've included a number of queries to show you what, and more importantly how, you can find answers in your data. As this repository is a learning experience for me I make no guaranties that they are the best, most efficient or idiomatic ways to achieve the results.

In some cases it's possible to change the returned results display from a text table to the clickable graph and back. If the RETURN has attributes, such as it will be displayed as text. If instead it's returned as the raw node RETURN p it will be shown as the graph. Changing the display mode may make navigating some of the results sets easier.

Basic Neo4J Queries

Show all data

Show the entire data set as a clickable graph.

MATCH (n) return n

Delete all data

Remove all the data, including nodes and relationships.

MATCH (n) detach delete n;

In the Python code you can achieve the same results with

  graph = Graph(host="localhost", password=neo_password)

People focused queries

Return everyone of a given role

If you want to find everyone with an exact role you can use the role name attribute. In this example we return everyone who is a Senior SRE

MATCH (p:Person)-[`is a`]-(r:Role {name:'Senior SRE'}) return as Employee, as Role

You can also match on sub strings inside an attribute. In the next query we will find everyone who is a Senior.

MATCH (p:Person)-[`is a`]-(r:Role)
RETURN p as Employee, r as Role

Graph view of all Seniors

And the same query results, but represented as a table

MATCH (p:Person)-[`is a`]-(r:Role)
RETURN as Employee, as Role

Table view of all Seniors

Show the tech leads

Show the tech lead from each team.

MATCH (p:Person)-[:`tech lead`]-(t:Team)
return as Employee, as Team
ORDER BY Employee

Team focused queries

Show team members

Show everyone in the given team as a table

MATCH (p:Person)-[:`assigned to`]-(t:Team {name:'Frontend'}) return,

and as a graph. In this view you can also see the other relationships. This can make further avenues of exploration easier.

MATCH (p:Person)-[:`assigned to`]-(t:Team {name:'Frontend'}) return p, t

Graph of Team relationships

Show seniors and their teams

Show everyone who is a Senior and the team they are in.

MATCH (p:Person)-[`is a`]-(r:Role)
MATCH (p)-[`assigned to`]-(t:Team)
RETURN as Employee, as Role, as Team

Show the services a team owns

Being able to determine the owner of a service is a basic but important building block for other, more interesting, queries.

MATCH (t:Team)-[`owns`]-(s:Service)
RETURN as Team, as Service

Graph version

MATCH (t:Team)-[`owns`]-(s:Service)
RETURN  t as Team, s as Service

Show the technical lead for services

MATCH (s:Service)-[:`owns`]-(t:Team)
MATCH (t:Team)-[:`tech lead`]-(p:Person)
RETURN as Service, as Team, as TechLead

And as a graph

MATCH (s:Service)-[:`owns`]-(t:Team)
MATCH (t:Team)-[:`tech lead`]-(p:Person)
RETURN s as Service, t as Team, p as TechLead

Graph of TechLead to Service relationships

Show all the team members responsible for a service

Sometimes you just need to find anyone involved in a service. This query will find everyone on the owning team.

MATCH (s:Service {name: 'Webchat'})-[:`owns`]-(t:Team)
MATCH (t:Team)-[:`assigned to`]-(p:Person)
RETURN as Service, as Owners

External Links

You can’t perform that action at this time.