Example of Cypher Query using Relationship design pattern.
Query:
MATCH (manager:Employee)<-[rel:PRIMARY_REPORTS_TO|DOTTED_REPORTS_TO]-(e:Employee)
WHERE manager.name = "Alice"
RETURN e.name AS employee,
type(rel) AS relationship_type,
rel.role AS role,
rel.start_date,
rel.end_date;
Sample Graph Data:
Assume we have the following graph:
Bob
reports toAlice
as the primary manager.Charlie
reports toAlice
as a dotted-line report for a project.Diana
reports toAlice
as a dotted-line report for mentorship.
Query Results:
employee | relationship_type | role | start_date | end_date |
---|---|---|---|---|
Bob | PRIMARY_REPORTS_TO | NULL | 2023-01-01 | NULL |
Charlie | DOTTED_REPORTS_TO | Project Lead | 2023-02-01 | 2024-01-31 |
Diana | DOTTED_REPORTS_TO | Mentor | 2023-03-15 | 2023-12-31 |
type(rel) AS relationship_type
-
Purpose of
type(rel)
:type(rel)
retrieves the name of the relationship type for each match.- In this query, it can return either:
PRIMARY_REPORTS_TO
DOTTED_REPORTS_TO
- This is useful when multiple types of relationships are being matched in the same query.
-
How It Works in the Query:
- The query matches any
rel
of typePRIMARY_REPORTS_TO
orDOTTED_REPORTS_TO
betweenAlice
(the manager) and her employees. - The
type(rel)
function determines whether the relationship is a primary or dotted-line report.
- The query matches any
-
Practical Use:
- It allows you to differentiate between primary and dotted-line reports in the result set.
- For instance, in the results above:
- Bob’s relationship is labeled as
PRIMARY_REPORTS_TO
. - Charlie and Diana’s relationships are labeled as
DOTTED_REPORTS_TO
.
- Bob’s relationship is labeled as
-
Handling NULL Values:
- In relationships like
PRIMARY_REPORTS_TO
, there may not be arole
property (e.g., Bob has norole
in his primary reporting line). This is reflected asNULL
in therole
column of the results.
- In relationships like
Why This Query is Powerful
- Insight Across Multiple Reporting Lines:
- Combines both primary and dotted-line reporting relationships into a single result set.
- Useful for identifying all employees managed by a specific manager, regardless of the type of reporting.
- Dynamic Role Attribution:
- The
rel.role
property allows capturing contextual information about the dotted-line relationships (e.g., “Project Lead” or “Mentor”).
- The
- Time-Bounded Relationships:
- The
start_date
andend_date
fields allow filtering based on active reporting periods. For example, you could modify the query to find employees who are currently reporting to Alice:MATCH (manager:Employee)<-[rel:PRIMARY_REPORTS_TO|DOTTED_REPORTS_TO]-(e:Employee) WHERE manager.name = "Alice" AND (rel.end_date IS NULL OR date(rel.end_date) >= date()) RETURN e.name AS employee, type(rel) AS relationship_type, rel.role, rel.start_date, rel.end_date;
- The