[cypher/en] proofread

This commit is contained in:
Boris Verkhovskiy 2024-04-20 09:05:03 -07:00
parent fe12d24e20
commit 097cd60e49

View File

@ -5,84 +5,57 @@ contributors:
- ["Théo Gauchoux", "https://github.com/TheoGauchoux"] - ["Théo Gauchoux", "https://github.com/TheoGauchoux"]
--- ---
Cypher is the Neo4js query language to manipulate graphs easily. It reuses syntax from SQL and mixes it with kind of ascii-art to represent graphs. Cypher is Neo4j's query language for easily manipulating graphs.
It reuses syntax from SQL and mixes it with kind of an ASCII-art to represent graphs.
This tutorial assumes that you already know graph concepts like nodes and relationships. This tutorial assumes that you already know graph concepts like nodes and relationships.
[Read more here.](https://neo4j.com/developer/cypher-query-language/) ## Nodes represent a record in a graph
`()` is an empty *node*, to indicate that there is a *node*, but it's not relevant for the query.
Nodes `(n)` is a *node* referred by the variable `n`, reusable in the query. It begins with lowercase and uses camelCase.
---
**Represents a record in a graph.** `(p:Person)` - you can add a *label* to your node, here `Person`. It's like a type/class/category. It begins with uppercase and uses camelCase.
`()` `(p:Person:Manager)` - a node can have many *labels*.
It's an empty *node*, to indicate that there is a *node*, but it's not relevant for the query.
`(n)` `(p:Person {name : 'Théo Gauchoux', age : 22})` - a node can have some *properties*, here `name` and `age`. It begins with lowercase and uses camelCase.
It's a *node* referred by the variable **n**, reusable in the query. It begins with lowercase and uses camelCase.
`(p:Person)` The types allowed in properties:
You can add a *label* to your node, here **Person**. It's like a type / a class / a category. It begins with uppercase and uses camelCase.
`(p:Person:Manager)` - Numeric
A node can have many *labels*. - Boolean
- String
- List of previous primitive types
`(p:Person {name : 'Théo Gauchoux', age : 22})` *Warning: there's no datetime properties in Cypher! You can use a String with a specific pattern or a Numeric from a specific date.*
A node can have some *properties*, here **name** and **age**. It begins with lowercase and uses camelCase.
The types allowed in properties : `p.name` - you can access a property with the dot style.
- Numeric ## Relationships (or Edges) connect two nodes
- Boolean
- String
- List of previous primitive types
*Warning : there isn't datetime property in Cypher ! You can use String with a specific pattern or a Numeric from a specific date.* `[:KNOWS]` is a *relationship* with the *label* `KNOWS`. It's a *label* as the node's label. It begins with uppercase and use UPPER\_SNAKE\_CASE.
`p.name` `[k:KNOWS]` - the same *relationship*, referred by the variable `k`, reusable in the query, but it's not necessary.
You can access to a property with the dot style.
`[k:KNOWS {since:2017}]` - the same *relationship*, with *properties* (like *node*), here `since`.
Relationships (or Edges) `[k:KNOWS*..4]` is structural information to use in a *path* (seen later). Here, `\*..4` says "Match the pattern, with the relationship `k` which can be repeated between 1 and 4 times.
---
**Connects two nodes** ## Paths - the way to mix nodes and relationships.
`[:KNOWS]` `(a:Person)-[:KNOWS]-(b:Person)` - a path describing that `a` and `b` know each other.
It's a *relationship* with the *label* **KNOWS**. It's a *label* as the node's label. It begins with uppercase and use UPPER\_SNAKE\_CASE.
`[k:KNOWS]` `(a:Person)-[:MANAGES]->(b:Person)` - a path can be directed. This path describes that `a` is the manager of `b`.
The same *relationship*, referred by the variable **k**, reusable in the query, but it's not necessary.
`[k:KNOWS {since:2017}]` `(a:Person)-[:KNOWS]-(b:Person)-[:KNOWS]-(c:Person)` - you can chain multiple relationships. This path describes the friend of a friend.
The same *relationship*, with *properties* (like *node*), here **since**.
`[k:KNOWS*..4]` `(a:Person)-[:MANAGES]->(b:Person)-[:MANAGES]->(c:Person)` - a chain can also be directed. This path describes that `a` is the boss of `b` and the big boss of `c`.
It's a structural information to use in a *path* (seen later). Here, **\*..4** says "Match the pattern, with the relationship **k** which be repeated between 1 and 4 times.
Commonly used patterns (from Neo4j documentation):
Paths ```cypher
--- // Friend-of-a-friend
**The way to mix nodes and relationships.**
`(a:Person)-[:KNOWS]-(b:Person)`
A path describing that **a** and **b** know each other.
`(a:Person)-[:MANAGES]->(b:Person)`
A path can be directed. This path describes that **a** is the manager of **b**.
`(a:Person)-[:KNOWS]-(b:Person)-[:KNOWS]-(c:Person)`
You can chain multiple relationships. This path describes the friend of a friend.
`(a:Person)-[:MANAGES]->(b:Person)-[:MANAGES]->(c:Person)`
A chain can also be directed. This path describes that **a** is the boss of **b** and the big boss of **c**.
Patterns often used (from Neo4j doc) :
```
// Friend-of-a-friend
(user)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) (user)-[:KNOWS]-(friend)-[:KNOWS]-(foaf)
// Shortest path // Shortest path
@ -91,17 +64,15 @@ path = shortestPath( (user)-[:KNOWS*..5]-(other) )
// Collaborative filtering // Collaborative filtering
(user)-[:PURCHASED]->(product)<-[:PURCHASED]-()-[:PURCHASED]->(otherProduct) (user)-[:PURCHASED]->(product)<-[:PURCHASED]-()-[:PURCHASED]->(otherProduct)
// Tree navigation // Tree navigation
(root)<-[:PARENT*]-(leaf:Category)-[:ITEM]->(data:Product) (root)<-[:PARENT*]-(leaf:Category)-[:ITEM]->(data:Product)
``` ```
## Create queries
Create queries
---
Create a new node Create a new node
``` ```cypher
CREATE (a:Person {name:"Théo Gauchoux"}) CREATE (a:Person {name:"Théo Gauchoux"})
RETURN a RETURN a
``` ```
@ -110,52 +81,51 @@ RETURN a
Create a new relationship (with 2 new nodes) Create a new relationship (with 2 new nodes)
``` ```cypher
CREATE (a:Person)-[k:KNOWS]-(b:Person) CREATE (a:Person)-[k:KNOWS]-(b:Person)
RETURN a,k,b RETURN a,k,b
``` ```
Match queries ## Match queries
---
Match all nodes Match all nodes
``` ```cypher
MATCH (n) MATCH (n)
RETURN n RETURN n
``` ```
Match nodes by label Match nodes by label
``` ```cypher
MATCH (a:Person) MATCH (a:Person)
RETURN a RETURN a
``` ```
Match nodes by label and property Match nodes by label and property
``` ```cypher
MATCH (a:Person {name:"Théo Gauchoux"}) MATCH (a:Person {name:"Théo Gauchoux"})
RETURN a RETURN a
``` ```
Match nodes according to relationships (undirected) Match nodes according to relationships (undirected)
``` ```cypher
MATCH (a)-[:KNOWS]-(b) MATCH (a)-[:KNOWS]-(b)
RETURN a,b RETURN a,b
``` ```
Match nodes according to relationships (directed) Match nodes according to relationships (directed)
``` ```cypher
MATCH (a)-[:MANAGES]->(b) MATCH (a)-[:MANAGES]->(b)
RETURN a,b RETURN a,b
``` ```
Match nodes with a `WHERE` clause Match nodes with a `WHERE` clause
``` ```cypher
MATCH (p:Person {name:"Théo Gauchoux"})-[s:LIVES_IN]->(city:City) MATCH (p:Person {name:"Théo Gauchoux"})-[s:LIVES_IN]->(city:City)
WHERE s.since = 2015 WHERE s.since = 2015
RETURN p,state RETURN p,state
@ -163,19 +133,17 @@ RETURN p,state
You can use `MATCH WHERE` clause with `CREATE` clause You can use `MATCH WHERE` clause with `CREATE` clause
``` ```cypher
MATCH (a), (b) MATCH (a), (b)
WHERE a.name = "Jacquie" AND b.name = "Michel" WHERE a.name = "Jacquie" AND b.name = "Michel"
CREATE (a)-[:KNOWS]-(b) CREATE (a)-[:KNOWS]-(b)
``` ```
## Update queries
Update queries
---
Update a specific property of a node Update a specific property of a node
``` ```cypher
MATCH (p:Person) MATCH (p:Person)
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
SET p.age = 23 SET p.age = 23
@ -183,7 +151,7 @@ SET p.age = 23
Replace all properties of a node Replace all properties of a node
``` ```cypher
MATCH (p:Person) MATCH (p:Person)
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
SET p = {name: "Michel", age: 23} SET p = {name: "Michel", age: 23}
@ -191,27 +159,25 @@ SET p = {name: "Michel", age: 23}
Add new property to a node Add new property to a node
``` ```cypher
MATCH (p:Person) MATCH (p:Person)
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
SET p + = {studies: "IT Engineering"} SET p += {studies: "IT Engineering"}
``` ```
Add a label to a node Add a label to a node
``` ```cypher
MATCH (p:Person) MATCH (p:Person)
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
SET p:Internship SET p:Internship
``` ```
## Delete queries
Delete queries
---
Delete a specific node (linked relationships must be deleted before) Delete a specific node (linked relationships must be deleted before)
``` ```cypher
MATCH (p:Person)-[relationship]-() MATCH (p:Person)-[relationship]-()
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
DELETE relationship, p DELETE relationship, p
@ -219,17 +185,17 @@ DELETE relationship, p
Remove a property in a specific node Remove a property in a specific node
``` ```cypher
MATCH (p:Person) MATCH (p:Person)
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
REMOVE p.age REMOVE p.age
``` ```
*Pay attention to the `REMOVE`keyword, it's not `DELETE` !* *Pay attention to the `REMOVE` keyword, it's not `DELETE`!*
Remove a label from a specific node Remove a label from a specific node
``` ```cypher
MATCH (p:Person) MATCH (p:Person)
WHERE p.name = "Théo Gauchoux" WHERE p.name = "Théo Gauchoux"
DELETE p:Person DELETE p:Person
@ -237,32 +203,28 @@ DELETE p:Person
Delete entire database Delete entire database
``` ```cypher
MATCH (n) MATCH (n)
OPTIONAL MATCH (n)-[r]-() OPTIONAL MATCH (n)-[r]-()
DELETE n, r DELETE n, r
``` ```
*Seriously, it's the `rm -rf /` of Cypher !* *Seriously, it's the `rm -rf /` of Cypher!*
## Other useful clauses
Other useful clauses `PROFILE` - before a query, show its execution plan.
---
`PROFILE` `COUNT(e)` - count entities (nodes or relationships) matching `e`.
Before a query, show the execution plan of it.
`COUNT(e)` `LIMIT x` - limit the result to the first `x` results.
Count entities (nodes or relationships) matching **e**.
`LIMIT x` ## Special hints
Limit the result to the x first results.
- Cypher only has single-line comments, using double-slashes: `// comment`
Special hints - You can execute a Cypher script stored in a .cql file directly in Neo4j (it's an import). However, you can't have multiple statements in this file (separated by `;`).
---
- There is just single-line comments in Cypher, with double-slash : // Comments
- You can execute a Cypher script stored in a **.cql** file directly in Neo4j (it's an import). However, you can't have multiple statements in this file (separated by **;**).
- Use the Neo4j shell to write Cypher, it's really awesome. - Use the Neo4j shell to write Cypher, it's really awesome.
- The Cypher will be the standard query language for all graph databases (known as **OpenCypher**). - Cypher will be the standard query language for all graph databases (known as [openCypher](https://opencypher.org/)).
Read more [here](https://neo4j.com/developer/cypher-query-language/).