Skip to content

Commit

Permalink
[doc] Cypher-ngql CN (vesoft-inc#1765)
Browse files Browse the repository at this point in the history
* Fix lambda captured by reference (vesoft-inc#1681)

* add cypher v.s. ngql Chinese edition

* update

* update

* update catelog

* update

* update

Co-authored-by: Doodle <13706157+critical27@users.noreply.github.com>
Co-authored-by: yaphet <darion.wang@vesoft.com>
Co-authored-by: dutor <440396+dutor@users.noreply.github.com>
  • Loading branch information
4 people authored Feb 19, 2020
1 parent 25a23b7 commit 19f6d5b
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 20 deletions.
155 changes: 155 additions & 0 deletions docs/manual-CN/5.appendix/cypher-ngql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Cypher 和 nGQL


## 基本概念对比

|概念名称 | Cypher | nGQL |
| --- | --- | --- |
| vertex, node | node | vertex |
| edge, relationship | relationship | edge |
| vertex type | label | tag |
| edge type | relationship type | edge type |
| vertex identifier | node id generated by default | vid |
| edge identifier | edge id generated by default |<src, dst, rank> |


## 图基本操作

|操作 | Cypher | nGQL |
| --- | ------------ | ------------ |
| 列出所有 labels/tags | * MATCH (n) RETURN distinct labels(n); <br/> * call db.labels(); | SHOW TAGS |
| 插入指定类型的点 | CREATE (:Person {age: 16}) | INSERT VERTEX <tag_name> (prop_name_list) VALUES \<vid>:(prop_value_list) |
| 插入指定类型的边| CREATE (src)-[:LIKES]->(dst) <br/> SET rel.prop = V | INSERT EDGE <edge_type> ( <prop_name_list> ) VALUES <src_vid> -> <dst_vid>[@<ranking>]: ( <prop_value_list> ) |
| 删除点 | MATCH (n) WHERE ID(n) = vid <br/> DETACH DELETE n | DELETE VERTEX \<vid> |
| 删除边 | MATCH ()-[r]->() WHERE ID(r)=edgeID <br/> DELETE r | DELETE EDGE <edge_type> \<src_vid> -> \<dst_vid>[@<ranking>] |
| 更新点属性 |SET n.name = V | UPDATE VERTEX \<vid> SET <update_columns> |
| 查询指定点的属性| MATCH (n) <br/> WHERE ID(n) = vid <br/> RETURN properties(n) | FETCH PROP ON <tag_name> \<vid>|
| 查询指定边的属性 | MATCH (n)-[r]->() <br/> WHERE ID(r)=edgeID <br/> return properties(r)| FETCH PROP ON <edge_type> <src_vid> -> <dst_vid>[@<ranking>] |
| 查询指定点的某一类关系 |MATCH (n)-[r:edge_type]->() WHERE ID(n) = vid| GO FROM \<vid> OVER \<edge_type> |
| 指定点的某一类反向关系 | MATCH (n)<-[r:edge_type]-() WHERE ID(n) = vid| GO FROM \<vid> OVER \<edge_type> REVERSELY |
| 指定点某一类关系第 N-Hop 查询 |MATCH (n)-[r:edge_type*N]->() <br/> WHERE ID(a) = vid <br/> return r | GO N STEPS FROM \<vid> OVER \<edge_type> |
| 两点路径 | MATCH p =(a)-[]->(b) <br/> WHERE ID(a) = a_vid AND ID(b) = b_vid <br/> RETURN p | FIND ALL PATH FROM \<a_vid> TO \<b_vid> OVER * |

## 示例查询

示例使用以下数据:

![image](https://user-images.githubusercontent.com/42762957/71503167-0e264b80-28af-11ea-87c5-76f4fd1275cd.png)

- 插入数据

```
# 插入点
nebula> INSERT VERTEX character(name, age, type) VALUES hash("saturn"):("saturn", 10000, "titan"), hash("jupiter"):("jupiter", 5000, "god");
# 插入边
nebula> INSERT EDGE father() VALUES hash("jupiter")->hash("saturn"):();
// cypher
cypher> CREATE (src:character {name:"saturn", age: 10000, type:"titan"})
> CREATE (dst:character {name:"jupiter", age: 5000, type:"god"})
> CREATE (src)-[rel:father]->(dst)
```


- 删除点

```
nebula> DELETE VERTEX hash("prometheus");
cypher> MATCH (n:character {name:"prometheus"})
> DETACH DELETE n
```

- 更新点的属性

```
nebula> UPDATE VERTEX hash("jesus") SET character.type = 'titan';
cypher> MATCH (n:character {name:"prometheus"})
> SET n.type = 'titan'
```

- 查看点的属性

```
nebula> FETCH PROP ON character hash("saturn");
===================================================
| character.name | character.age | character.type |
===================================================
| saturn | 10000 | titan |
---------------------------------------------------
cypher> MATCH (n:character {name:"prometheus"})
> RETURN properties(n)
```

- 查询 hercules 祖父的姓名

```
nebula> GO 2 STEPS FROM hash("hercules") OVER father YIELD $$.character.name;
=====================
| $$.character.name |
=====================
| saturn |
---------------------
cypher> MATCH (src:character{name:"prometheus"})-[r:father*2]->(dst:character)
> RETURN dst.name;
```

- 查询 hercules 父亲的姓名

```
nebula> GO FROM hash("hercules") OVER father YIELD $$.character.name;
=====================
| $$.character.name |
=====================
| jupiter |
---------------------
cypher> MATCH (src:character{name:"prometheus"})-[r:father]->(dst:character)
> RETURN dst.name
```

- 查询百岁老人的姓名

```
nebula> # coming soon
cypher> MATCH (src:character)
> WHERE src.age > 100
> RETURN src.name
```

- 找出 pluto 和谁住

```
nebula> GO FROM hash("pluto") OVER lives YIELD lives._dst AS place | GO FROM $-.place OVER lives REVERSELY WHERE \
> $$.character.name != "pluto" YIELD $$.character.name AS cohabitants;
===============
| cohabitants |
===============
| cerberus |
---------------
cypher> MATCH (src:character{name:"pluto"})-[r1:lives]->()<-[r2:lives]-(dst:character)
> RETURN dst.name
```

- 查询 Pluto 的兄弟们以及他们的居住地

```
nebula> GO FROM hash("pluto") OVER brother YIELD brother._dst AS god | \
> GO FROM $-.god OVER lives YIELD $^.character.name AS Brother, $$.location.name AS Habitations;
=========================
| Brother | Habitations |
=========================
| jupiter | sky |
-------------------------
| neptune | sea |
-------------------------
cypher> MATCH (src:Character{name:"pluto"})-[r1:brother]->(bro:Character)-[r2:lives]->(dst)
> RETURN bro.name, dst.name
```
1 change: 1 addition & 0 deletions docs/manual-CN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
## 附录

* [Gremlin VS nGQL](5.appendix/gremlin-ngql.md)
* [Cypher V.S. nGQL](5.appendix/cypher-ngql.md)

## 其他

Expand Down
36 changes: 18 additions & 18 deletions docs/manual-EN/5.appendix/cypher-ngql.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@

## Basic Graph Operations

Name | Cypher | nGQL |
|Operations | Cypher | nGQL |
| --- | ------------ | ------------ |
| List all labels/tags | * MATCH (n) RETURN distinct labels(n); <br/> * call db.labels(); | SHOW TAGS |
| Insert a vertex with a specified type | CREATE (:Person {age: 16}) | INSERT VERTEX <tag_name> (prop_name_list) VALUES \<vid>:(prop_value_list) |
| Insert an edge with specified edge type | CREATE (src)-[:LIKES]->(dst) <br/> SET rel.prop = V | INSERT EDGE <edge_name> ( <prop_name_list> ) VALUES <src_vid> -> <dst_vid>: ( <prop_value_list> ) |
| Delete a vertex | MATCH (n:A) <br/> DETACH DELETE n | DELETE VERTEX \<vid> |
| Delete an edge | MATCH ()-[r:LIKES]->() <br/> DELETE r | DELETE EDGE <edge_type> \<src_vid> -> \<dst_vid> |
| Insert an edge with specified edge type | CREATE (src)-[rel:LIKES]->(dst) <br/> SET rel.prop = V | INSERT EDGE <edge_type> ( <prop_name_list> ) VALUES <src_vid> -> <dst_vid>[@<ranking>]: ( <prop_value_list> ) |
| Delete a vertex | MATCH (n) WHERE ID(n) = vid <br/> DETACH DELETE n | DELETE VERTEX \<vid> |
| Delete an edge | MATCH ()-[r]->() WHERE ID(r)=edgeID <br/> DELETE r | DELETE EDGE <edge_type> \<src_vid> -> \<dst_vid>[@<ranking>] |
| Update a vertex property |SET n.name = V | UPDATE VERTEX \<vid> SET <update_columns> |
| Fetch vertice prop| MATCH (n) <br/> WHERE id(n) = vid <br/> RETURN properties(n) | FETCH PROP ON <tag_name> \<vid>|
| Fetch edges prop | MATCH (n)-[r:FOLLOW]->() <br/> WHERE id(n)=vid <br/> return properties(r)| FETCH PROP ON <edge_name> <src_vid> -> <dst_vid> |
| Query a vertex along specified edge type |MATCH (n)-[r:FOLLOW]->() | GO FROM \<vid> OVER \<edge> |
| Query a vertex along specified edge type reversely | MATCH (n)<-[r:FOLLOW]-()| GO FROM \<vid> OVER \<edge> REVERSELY |
| Query N hops along a specified edge |MATCH (n)-[r:FOLLOW*1..N]->() <br/> WHERE id(a) = vid <br/> return r | GO N STEPS FROM \<vid> OVER \<edge> |
| Find path between two vertices |MATCH p =(a)-[]->(b) <br/> WHERE id(a) = vid <br/> RETURN p | FIND ALL PATH FROM \<vid> TO \<vid> OVER * |
| Fetch vertice prop| MATCH (n) <br/> WHERE ID(n) = vid <br/> RETURN properties(n) | FETCH PROP ON <tag_name> \<vid>|
| Fetch edges prop | MATCH (n)-[r]->() <br/> WHERE ID(r)=edgeID <br/> return properties(r)| FETCH PROP ON <edge_type> <src_vid> -> <dst_vid>[@<ranking>]|
| Query a vertex along specified edge type |MATCH (n)-[r:edge_type]->() WHERE ID(n) = vid| GO FROM \<vid> OVER \<edge_type> |
| Query a vertex along specified edge type reversely | MATCH (n)<-[r:edge_type]-() WHERE ID(n) = vid | GO FROM \<vid> OVER \<edge_type> REVERSELY |
| Get the N-Hop along a specified edge type |MATCH (n)-[r:edge_type*N]->() <br/> WHERE ID(a) = vid <br/> return r | GO N STEPS FROM \<vid> OVER \<edge_type> |
| Find path between two vertices | MATCH p =(a)-[]->(b) <br/> WHERE ID(a) = a_vid AND ID(b) = b_vid <br/> RETURN p | FIND ALL PATH FROM \<a_vid> TO \<b_vid> OVER * |

## Example Queries

Expand Down Expand Up @@ -67,10 +67,10 @@ cypher> MATCH (n:character {name:"prometheus"})
nebula> UPDATE VERTEX hash("jesus") SET character.type = 'titan';
cypher> MATCH (n:character {name:"prometheus"})
> SET n.type = 'titan';
> SET n.type = 'titan'
```

- Fetch data
- Fetch vertice properties

```
nebula> FETCH PROP ON character hash("saturn");
Expand All @@ -81,7 +81,7 @@ nebula> FETCH PROP ON character hash("saturn");
---------------------------------------------------
cypher> MATCH (n:character {name:"prometheus"})
> RETURN properties(n);
> RETURN properties(n)
```

- Find the name of hercules's grandfather
Expand All @@ -95,7 +95,7 @@ nebula> GO 2 STEPS FROM hash("hercules") OVER father YIELD $$.character.name;
---------------------
cypher> MATCH (src:character{name:"prometheus"})-[r:father*2]->(dst:character)
> RETURN dst.name;
> RETURN dst.name
```

- Find the name of hercules's father
Expand All @@ -112,17 +112,17 @@ cypher> MATCH (src:character{name:"prometheus"})-[r:father]->(dst:character)
> RETURN dst.name
```

- Find the characters with age > 100
- Find the the centenarians' name.

```
nebula> XXX # not supported yet
nebula> # coming soon
cypher> MATCH (src:character)
> WHERE src.age > 100
> RETURN src.name
```

- Find who are pluto's cohabitants
- Find who live with pluto

```
nebula> GO FROM hash("pluto") OVER lives YIELD lives._dst AS place | GO FROM $-.place OVER lives REVERSELY WHERE \
Expand All @@ -137,7 +137,7 @@ cypher> MATCH (src:character{name:"pluto"})-[r1:lives]->()<-[r2:lives]-(dst:char
> RETURN dst.name
```

- Find pluto's brother and their live places?
- Find pluto's brother and their habitations?

```
nebula> GO FROM hash("pluto") OVER brother YIELD brother._dst AS god | \
Expand Down
2 changes: 1 addition & 1 deletion docs/manual-EN/5.appendix/gremlin-ngql.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ gremlin> g.V(jesus).property('age', 6000);
- Find the characters with age > 100
```bash
nebula> XXX # not supported yet
nebula> # coming soon
gremlin> g.V().hasLabel('character').has('age',gt(100)).values('name');
==>saturn
==>jupiter
Expand Down
3 changes: 2 additions & 1 deletion docs/manual-EN/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ It is the optimal solution in the world capable of hosting graphs with dozens of

## Appendix

* [Gremlin VS nGQL](5.appendix/gremlin-ngql.md)
* [Gremlin V.S. nGQL](5.appendix/gremlin-ngql.md)
* [Cypher V.S. nGQL](5.appendix/cypher-ngql.md)

## Misc

Expand Down

0 comments on commit 19f6d5b

Please sign in to comment.