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:
Bobreports toAliceas the primary manager.Charliereports toAliceas a dotted-line report for a project.Dianareports toAliceas 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_TODOTTED_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
relof typePRIMARY_REPORTS_TOorDOTTED_REPORTS_TObetweenAlice(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 aroleproperty (e.g., Bob has norolein his primary reporting line). This is reflected asNULLin therolecolumn 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.roleproperty allows capturing contextual information about the dotted-line relationships (e.g., “Project Lead” or “Mentor”).
- The
- Time-Bounded Relationships:
- The
start_dateandend_datefields 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










