Indirect relationships between nodes

Introduction

In this lesson, you will learn how to refactor your graph by adding indirect relationships between nodes

Adding indirect relationships between nodes can help improve traversal performance and create more efficient query patterns.

mermaid
Indirect relationships between nodes
graph TD
    Node1 -->|RELATIONSHIP| Node2
    Node2 -->|RELATIONSHIP| Node3
    Node1 -->|<i>INDIRECT_RELATIONSHIP</i>| Node3
    
    Node1(("<b>Node</b>"))
    
    Node2(("<b>Node</b>"))
    
    Node3(("<b>Node</b>"))

    %% Style the new relationship
    linkStyle 2 stroke-dasharray: 5 5

Employees in regions

Employees are associated with regions through their territories.

To find all employees in a region, you need to traverse from the Employee to the Territory and then to the Region.

cypher
Employees in regions
MATCH (employee:Employee)-[:IN_TERRITORY]->(:Territory)-[:IN_REGION]->(region:Region)
RETURN DISTINCT employee.firstName, region.regionDescription

An indirect relationship

To optimize this query, you can create an indirect relationship between the Employee and the Region nodes.

mermaid
Indirect PART_OF_REGION relationship
graph TD
    Territory -->|IN_REGION| Region
    Employee -->|IN_TERRITORY| Territory
    Employee -->|<i>PART_OF_REGION</i>| Region
    
    Territory(("<b>Territory</b>"))
    
    Region(("<b>Region</b>"))
    
    Employee(("<b>Employee</b>"))

    %% Style the new relationship
    linkStyle 2 stroke-dasharray: 5 5

Create the indirect relationship

The following Cypher query creates a PART_OF_REGION relationship between Employee and Region nodes based on the existing relationships:

cypher
Create PART_OF_REGION relationship
MATCH (employee:Employee)-[:IN_TERRITORY]->(:Territory)-[:IN_REGION]->(region:Region)  // (1)
MERGE (employee)-[p:PART_OF_REGION]->(region)                                          // (2)
RETURN employee, p, region                                                             // (3)
  1. Find the Employee, and Region nodes through the existing IN_TERRITORY and IN_REGION relationships.

  2. Create a new PART_OF_REGION relationship between the Employee and Region nodes.

  3. Return the employee, the new relationship, and the region to verify

Query the new relationship

You can now query for employees in regions using the new PART_OF_REGION relationship:

cypher
Employees in regions
MATCH (employee:Employee)-[:PART_OF_REGION]->(region:Region)
RETURN employee.firstName, region.regionDescription

Profile the original query and the refactored query to compare their performance.

Deleting relationships

You can delete relationships using the DELETE clause. For example, to delete all PART_OF_REGION relationships:

cypher
Delete PART_OF_REGION relationships
MATCH (employee)-[p:PART_OF_REGION]->(region)
DELETE p

Employees and customers

Orders are connected to employees through the SOLD relationship and to customers through the PURCHASED relationship.

You challenge is to create an indirect SOLD_TO relationship between the Employee that sold an Order and the Customer who purchased the Order.

mermaid
SOLD_TO relationship between employees and customers
graph TD
    Customer -->|PURCHASED| Order
    Employee -->|SOLD| Order
    Employee -->|<i>SOLD_TO</i>| Customer
    
    Order(("<b>Order</b>"))
    
    Customer(("<b>Customer</b>"))
    
    Employee(("<b>Employee</b>"))
    
    %% Style the new relationship
    linkStyle 2 stroke-dasharray: 5 5

Your challenge

To create the (Employee)-[:SOLD_TO]→(Customer) relationship you will need to:

  1. Identify the employees and customers connected through orders.

  2. Create a new SOLD_TO relationship between the employee and customer nodes.

  3. Query the new relationship to verify it was created correctly.

Profile the original query and the refactored query to compare their performance.

Click to reveal the solution
  1. Identify the employees and customers connected through orders

    cypher
    Employees and customer relationships
    MATCH (employee:Employee)-[:SOLD]->(:Order)<-[:PURCHASED]-(customer:Customer)
    RETURN DISTINCT employee.firstName, customer.companyName
  2. Create a new SOLD_TO relationship between the employee and customer nodes

    cypher
    Create indirect <code>SOLD_TO</code> relationships
    MATCH (employee:Employee)-[:SOLD]->(:Order)<-[:PURCHASED]-(customer:Customer)
    MERGE (employee)-[s:SOLD_TO]->(customer)
    RETURN employee, s, customer
  3. Query the new relationship to verify it was created correctly

    cypher
    Employees and the customers they sold to
    MATCH (employee:Employee)-[s:SOLD_TO]->(customer:Customer)
    RETURN employee.firstName, customer.companyName

Lesson Summary

In this lesson, you learned about creating indirect relationships between nodes to optimize graph traversals and improve query performance.

In the next lesson, you will learn about normalizing properties to nodes.

Chatbot

How can I help you today?