Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc]add bidirect #1752

Merged
merged 4 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
`GO` 是 **Nebula Graph** 中最常用的关键字,可以指定过滤条件(如 `WHERE`)遍历图数据并获取点和边的属性,还能以指定顺序(`ORDER BY ASC | DESC`)返回指定数目(`LIMIT`)的结果。

>`GO` 的用法与 SQL 中的 `SELECT` 类似,重要区别是 `GO` 必须从遍历一系列的节点开始。
<!-- >请参考`FIND`的用法,它对应于SQL中的`SELECT`。 -->

```ngql
GO [ <N> STEPS ] FROM <node_list>
OVER <edge_type_list> [REVERSELY]
OVER <edge_type_list> [REVERSELY] [BIDIRECT]
[ WHERE <expression> [ AND | OR expression ...]) ]
YIELD | YIELDS [DISTINCT] <return_list>

Expand All @@ -22,12 +21,11 @@
<col_name> [AS <col_alias>] [, <col_name> [AS <col_alias>] ...]
```

* [ <N> STEPS ] 指定查询 N 跳。
* [ \<N> STEPS ] 指定查询 N 跳。
* <node_list> 为逗号隔开的节点 ID,或特殊占位符 `$-.id` (参看 `PIPE` 用法)。
* <edge_type_list> 为图遍历返回的边类型列表。
* [ WHERE <expression> ] 指定被筛选的逻辑条件,WHERE 可用于起点,边及终点,同样支持逻辑关键词 AND、OR、NOT,详情参见 [WHERE](where-syntax.md) 的用法。
* YIELD [DISTINCT] <return_list> 以列的形式返回结果,并可对列进行重命名。详情参看 `YIELD`
用法。`DISTINCT` 的用法与 SQL 相同。
* [ WHERE \<expression> ] 指定被筛选的逻辑条件,WHERE 可用于起点,边及终点,同样支持逻辑关键词 AND、OR、NOT,详情参见 WHERE 的用法。
* YIELD [DISTINCT] <return_list> 以列的形式返回结果,并可对列进行重命名。详情参看 `YIELD` 用法。`DISTINCT` 的用法与 SQL 相同。

## 示例

Expand Down Expand Up @@ -150,7 +148,11 @@ nebula> GO FROM 100 OVER follow, serve YIELD follow.degree, serve.start_year;
例如:

```ngql
nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._src AS id | \
nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._src -- 返回 100
```

```ngql
nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._dst AS id | \
GO FROM $-.id OVER serve WHERE $^.player.age > 20 YIELD $^.player.name AS FriendOf, $$.team.name AS Team;

============================
Expand All @@ -163,3 +165,31 @@ nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._src AS id | \
```

遍历所有关注 100 号球员的球员,找出这些球员服役的球队,筛选年龄大于 20 岁的球员并返回这些球员姓名和其服役的球队名称。如果此处不指定 `YIELD`,则默认返回每条边目标点的 `vid`。

## 双向遍历

目前 **Nebula Graph** 支持使用关键词 `BIDIRECT` 进行双向遍历,语法为:

```ngql
GO FROM <node_list>
OVER <edge_type_list> BIDIRECT
WHERE (expression [ AND | OR expression ...])  
YIELD | YIELDS  [DISTINCT] <return_list>
```

例如:

```ngql
nebula> GO FROM 102 OVER follow BIDIRECT
amber-moe marked this conversation as resolved.
Show resolved Hide resolved
===============
| follow._dst |
===============
| 101 |
---------------
| 103 |
---------------
| 135 |
---------------
```

上述语句同时返回 102 关注的球员及关注 102 的球员。
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
It indicates to traverse in a graph with specific filters (the `WHERE` clause), to fetch properties of vertices and edges, and return results (the `YIELD` clause) with given order (the `ORDER BY ASC | DESC` clause) and numbers (the `LIMIT` clause).

> The syntax of `GO` statement is very similar to `SELECT` in SQL. Notice that the major difference is that `GO` must start traversing from a (set of) vertex (vertices).
<!-- >You can refer to `FIND` statement (in progress), which is the counterpart of `SELECT` in SQL. -->

```ngql
GO [ <N> STEPS ] FROM <node_list>
OVER <edge_type_list> [REVERSELY]
OVER <edge_type_list> [REVERSELY] [BIDIRECT]
[ WHERE <expression> [ AND | OR expression ...]) ]
YIELD | YIELDS [DISTINCT] <return_list>

Expand All @@ -25,11 +24,11 @@ It indicates to traverse in a graph with specific filters (the `WHERE` clause),
<col_name> [AS <col_alias>] [, <col_name> [AS <col_alias>] ...]
```

* [ <N> STEPS ] specifies the N query hops
* <node_list> is either a list of vertices' vids separated by comma(,), or a special place holder `$-.id` (refer `PIPE` syntax).
* [ \<N> STEPS ] specifies the N query hops
* <node_list> is either a list of node's vid separated by comma(,), or a special place holder `$-.id` (refer `PIPE` syntax).
* <edge_type_list> is a list of edge types which graph traversal can go through.
* [ WHERE <expression> ] extracts only those results that fulfill the specified conditions. WHERE syntax can be conditions for src-vertex, the edges, and dst-vertex. The logical AND, OR, NOT are also supported. See [WHERE Syntax](where-syntax.md) for more information.
* YIELD [DISTINCT] <return_list> statement returns the result in column format and rename as an alias name. See `YIELD`-syntax for more information. The `DISTINCT` syntax works the same as SQL.
* [ WHERE \<expression> ] extracts only those results that fulfill the specified conditions. WHERE syntax can be conditions for src-vertex, the edges, and dst-vertex. The logical AND, OR, NOT are also supported. See [WHERE Syntax](where-syntax.md) for more information.
* YIELD [DISTINCT] <return_list> statement returns the result in column format and rename as an alias name. See `YIELD` syntax for more information. The `DISTINCT` syntax works the same as SQL.

## Examples

Expand Down Expand Up @@ -153,7 +152,11 @@ Currently, **Nebula Graph** supports traversing reversely using keyword `REVERSE
For example:

```ngql
nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._src AS id | \
nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._src -- returns 100
amber-moe marked this conversation as resolved.
Show resolved Hide resolved
```

```ngql
nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._dst AS id | \
GO FROM $-.id OVER serve WHERE $^.player.age > 20 YIELD $^.player.name AS FriendOf, $$.team.name AS Team;

============================
Expand All @@ -165,4 +168,32 @@ nebula> GO FROM 100 OVER follow REVERSELY YIELD follow._src AS id | \
----------------------------
```

The above query first traverses players that follow player 100 and finds the teams they serve, then filter players who are older than 20, and finally it returns their names and teams. Of course, you can query without specifying `YIELD`, which will return the `vids` of the dest vertices of each edge by default.
The above query first traverses players that follow player 100 and finds the teams they serve, then filter players who are older than 20, and finally it returns their names and teams. Of course, you can query without specifying YIELD, which will return the vids of the dest vertices of each edge by default.

## Traverse Bidirect

Currently, **Nebula Graph** supports traversing along in and out edges using keyword `BIDIRECT`, the syntax is:

```ngql
GO FROM <node_list>
OVER <edge_type_list> BIDIRECT
WHERE (expression [ AND | OR expression ...])  
YIELD | YIELDS  [DISTINCT] <return_list>
```

For example:

```ngql
nebula> GO FROM 102 OVER follow BIDIRECT
amber-moe marked this conversation as resolved.
Show resolved Hide resolved
===============
| follow._dst |
===============
| 101 |
---------------
| 103 |
---------------
| 135 |
---------------
```

The above query returns players followed by 102 and follow 102 at the same time.