• Home
  • Project
    Introduction
    • Introduction
    • Background and Motivation
    • What is a Good Timetable?
    • Project Aims and Scope
  • Graph Data
    Model
    • Graph vs Relational Data Models
    • Graph Data Model for Timetabling
    • Early Insights
    • Model Expansion
    • Graphing Time
  • Data
    Pipeline
    • ETL Overview
    • Approach
    • Configuration and Logging
    • Extract
    • Transform
    • Google Drive Load
    • Neo4j Load
    • Reflection
  • Timetable
    Metrics
    • Timetable Metrics
    • Metric Aggregations
    • Implementing Metrics
    • TQI Summary
  • Final
    Thoughts
  • Appendices
    & Extras
    • Appendix Table of Contents
    • References
    • Acknowledgements
  • Word
  1. Appendices
  2. Neo4j & Cypher Code
  3. General Queries
  • Home
  • Project Introduction
    • Introduction
    • Background and Motivation
    • What is a Good Timetable?
    • Project Aims and Scope
  • Graph Data Model
    • Graph vs Relational Data Models
    • Graph Data Model for Timetabling
    • Early Insights
    • Model Expansion
    • Graphing Time
  • Data Pipeline
    • ETL Overview
    • Approach
    • Configuration and Logging
    • Extract
    • Transform
    • Google Drive Load
    • Neo4j Load
    • Reflection
  • Timetable Metrics
    • Timetable Metrics
    • Metric Aggregations
    • Implementing Metrics
    • TQI Summary
  • Final Thoughts
  • Appendices
    • Random Graph Generator
    • Technology Stack
    • Configuration
    • Anonymisation
    • ETL Summary and Code
      • ETL Summary
      • ETL Code
      • Config and Misc
      • Extract-SQL
      • Extract
      • Google Drive Load
      • Transform
      • Neo4j Load
    • Neo4j & Cypher Code
      • Cypher Queries
      • Creating Nodes and Relationships
      • Deleting Nodes and Relationships
      • General Queries
      • Count Queries
      • Hard (timetabling) Constraints
      • Student Clashes
      • Soft Constraints
      • Rooms and Spaces
      • Perspectives
      • Blue Skies Opportunities
  • Supervision
    • Supervision
    • Notes Example 1
    • Notes Example 2
    • Notes Example 3
  • References
  • Acknowledgements

On this page

  • List all nodes
  • datatype of property
  • unique properties
  • node labels without relationships
  • nodes without relationships - aka orphans
  • students without activities
  • activityType without activity
  • activities without rooms
  1. Appendices
  2. Neo4j & Cypher Code
  3. General Queries

P: General Queries


This page contains a selection of general queries which can be used to explore the graph database. The queries are designed to provide insights into the data and relationships between nodes.

List all nodes

The following query lists all nodes in the graph.

Caution

Consider size of graph and limits in settings before running this query.

MATCH (n)
RETURN n

All Nodes

All Nodes

datatype of property

Properties in a graph have datatypes which will enable different operations and there for insights. The query below returns the datatype of a node property.

/* return datatype of actStartTime on activity node */

MATCH (a:activity)
RETURN DISTINCT apoc.meta.cypher.type(a.actStartTime) as actStartTimeType

Datatype of Property

Datatype of Property

unique properties

A node or relationship can potentially have many properties. The query below lists the properties of a node - in this case, activity.

// List unique properties for a Node

MATCH (a:activity)
UNWIND keys(a) AS propertyKey
RETURN COLLECT(DISTINCT propertyKey) AS propertyKeys
//RETURN DISTINCT propertyKey as propertyKeys

Unique Property Values

Unique Property Values

node labels without relationships

Graph databases are all about the relationships between nodes. It can be useful identifying nodes without relationships as they could indicate a problem with the data, data loading mechanism or be the outliers you want to identify.

For example, in a timetabling scenario, we would expect all nodes to be related to another node. However, we can see that several node labels are orphans. In the proof-of-concept, these results are expected or deliberate, due to the source data.

// Find node labels without relationships and their count

MATCH (n)
WHERE NOT EXISTS(()-[]-(n)) AND NOT EXISTS((n)-[]-())
RETURN DISTINCT labels(n) AS nodeLabels, count(n) AS nodeCount

Node labels without Relationships

Node labels without Relationships

nodes without relationships - aka orphans

Instead of returning a count of nodes without relationships per node label we can return the nodes as a graph or a table:

// Find nodes without relationships

MATCH (n)
WHERE NOT EXISTS(()-[]-(n)) AND NOT EXISTS((n)-[]-())
RETURN n

Nodes Without Relationships

Nodes Without Relationships

students without activities

In the timetabling context, we would expect students to be allocated to activities. It turns out that we have 219 students without activities. A bit more investigation indidates that they are all from a particular programme of study run.

// Students without Activities
MATCH (s:student)
WHERE NOT (s)-[:ATTENDS]->()
RETURN s

Students Without Activities

Students Without Activities

activityType without activity

Activities can have a activity type - the graph model could have activity type as a property or as a relationship. The query below finds activity typewithout activity. The decision may be to delete these orphaned nodes as they may cause problems with some calculations. They would be created if they become required in the future.

// Activities without Rooms

MATCH (at:activityType)
WHERE NOT (at)<-[:HAS_TYPE]-()
RETURN at;

ActivityType Without Room

ActivityType Without Room

activities without rooms

The graph has over 1500 activity instances without rooms. Most of these will be deliberate - online, virtual sessions - but we may want to query the graph to identify those where a room is expected.

// Activities without Rooms

MATCH (a:activity)
WHERE NOT (a)-[]->(:room)
RETURN a;

Activities Without Rooms

Activities Without Rooms
Deleting Nodes and Relationships
Count Queries

Copyright 2024, Petter Lövehagen

 

Built with Quarto