-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappendix-cypher.qmd
70 lines (48 loc) · 3.11 KB
/
appendix-cypher.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: "M: Cypher Queries"
---
<br> These pages collate Cypher[^10] queries used in the development of this project. The queries are grouped by the type of operation they perform, such as creating nodes, relationships, or querying the graph. The queries are presented in a format that can be copied and pasted directly into a Neo4j browser or other Cypher-compatible interface.
[^10]: "**Cypher** is a declarative graph query language that allows for expressive and efficient data querying in a property graph" (Wikipedia contributors, 2024)
They represent a *starting point* for further exploration and development of the graph database. The queries are not exhaustive, and there are many more possibilities for querying and analysing the data.
## Constraints
Constraints in Neo4j are used to enforce rules on the graph data, such as ensuring that certain properties are unique or that nodes have specific properties. Constraints can be used to maintain data integrity and prevent duplicate or inconsistent data. For example, we would want to enforce uniqueness constraints on most nodes and relationships, so that we do not duplicate students or allocations, etc.

## Indexes
Indexes in Neo4j are used to speed up queries by allowing the database to quickly locate nodes or relationships based on a property value. They will depend heavily on graph structure and specific use-cases. For example, if performing regular searches on `(activity{actStartTime})` it is probably beneficial to create an index on this property.
Search performance indexes include `RANGE`, `FULLTEXT`, and `POINT`. Range indexes are the default and support most queries. Text indexes, like `FULLTEXT`, are used for `string`-based searches and optimised for queries containing operators like `CONTAINS` or `STARTS WITH`. Point indexes are used for spatial queries and are optimised for `latitude` and `longitude` properties.
**Indexes in Graph**
```{python}
from connect_to_neo4j_db import connect_to_neo4j
from neo4j import GraphDatabase
# connect to Neo4j
driver = connect_to_neo4j()
# session
session = driver.session()
# run query
query = """
SHOW INDEXES;
"""
print("Running query...\n")
result = session.run(query)
for record in result:
print(record)
```
### Creating Indexes
This is the general syntax for creating an INDEX and some examples.
```{cypher}{.scroll-cypher}
// General syntax
CREATE INDEX FOR (n:NodeLabel) ON (n.propertyName)
// Examples
CREATE INDEX FOR (a:activity) ON (a.actStartDate);
CREATE INDEX FOR (a:activity) ON (a.actStartTime);
CREATE INDEX FOR (a:activity) ON (a.actEndTime);
CREATE INDEX FOR (a:activity) ON (a.actModSplusID);
CREATE INDEX FOR (a:activity) ON (a.id);
// composite index for clashes
CREATE INDEX activity_clash_index FOR (a:activity) ON (a.actStartDate, a.actStartTime, a.actEndTime);
CREATE INDEX FOR (r:room) ON (r.roomName);
CREATE INDEX FOR (r:room) ON (r.roomLatitude);
CREATE INDEX FOR (r:room) ON (r.roomLongitude);
CREATE SPATIAL INDEX room_location_index FOR (r:demoRoom) ON (r.location)
CREATE SPATIAL INDEX demoroom_location_index FOR (r:room) ON (r.location)
```