Adding specific node labels

Introduction

In this lesson, you will learn how to refactor your graph by adding specific node labels to improve query performance and semantic clarity.

Labels allow you to categorize nodes and relationships in your graph, making it easier to query and understand the structure of your data.

By adding specific labels to nodes, you can optimize query performance by reducing the search space and improving index utilization.

Managers

Currently, all employees are represented with the Employee label.

A manager can only be identified by the existence of a REPORTS_TO relationship.

mermaid
Employee REPORTS_TO Employee
graph TD
    Employee -->|REPORTS_TO| Manager
    
    Employee(("<b>Employee</b>"))
    
    Manager(("<b>Employee</b><br /><b><i>Manager</i></b>"))

Finding Managers

This query traverses all the Employee nodes and the REPORTS_TO relationships to find the managers.

cypher
Find the managers
MATCH (e:Employee)-[:REPORTS_TO]->(manager:Employee)
RETURN manager.firstName

By adding a Manager label to the manager nodes, you can optimize this query.

Adding a Manager label

Set a Manager label to the Employee nodes that have an incoming REPORTS_TO relationship:

cypher
Set Manager label
MATCH (e:Employee)-[:REPORTS_TO]->(manager:Employee)
SET manager:Manager
RETURN manager

Query the Manager label

You can now find all managers by querying for nodes with the Manager label:

cypher
Find all managers
MATCH (manager:Manager)
RETURN manager.firstName

Compare the PROFILE of the original query and the refactored query to see the performance difference.

Removing labels

You can remove a label from a node using the REMOVE clause. For example, to remove the Manager label from all nodes:

cypher
Remove Manager label
MATCH (e:Employee)-[:REPORTS_TO]->(manager:Employee)
REMOVE manager:Manager
RETURN manager

Adding a ProductDiscontinued label

Your challenge is to create an ProductActive label.

Not all products are still available for sale. The discontinued property on the Product nodes indicates whether a product has been discontinued or not.

cypher
Find all discontinued products
MATCH (p:Product)
WHERE p.discontinued = true
RETURN p.productName

To complete the challenge you will need to:

  1. Create a Cypher query to identify the active products.

  2. Update the query to SET a new ProductActive label on the active products.

  3. Write a new query to find all active products using the new ProductActive label.

Profile the queries before and after to see the performance difference.

Click to reveal the solution
  1. You can identify the active products by checking for discontinued = FALSE:

    cypher
    Find active products
    MATCH (p:Product)
    WHERE p.discontinued = false
    RETURN p.productName
  2. You can set the ProductActive label on the active products:

    cypher
    Set the label
    MATCH (p:Product)
    WHERE p.discontinued = false
    SET p:ProductActive
    RETURN p.productName
  3. Query for active products using the new label:

    cypher
    Find <code>ProductActive</code> nodes
    MATCH (p:ProductActive)
    RETURN p.productName

Finding discontinued products

To find all discontinued products, you can query for Product nodes and do not have the ProductActive label:

cypher
Find discontinued products
MATCH (p:Product&!ProductActive)
RETURN p.productName

Lesson Summary

In this lesson, you learned about adding specific node labels to improve query performance and make your graph model more semantically clear.

In the next lesson, you will learn about adding indirect relationships between nodes.

Chatbot

How can I help you today?