GO

GO用指定的过滤条件遍历图,并返回结果。

openCypher兼容性

本文操作仅适用于原生nGQL。

语法

  1. GO [[<M> TO] <N> STEPS ] FROM <vertex_list>
  2. OVER <edge_type_list> [{REVERSELY | BIDIRECT}]
  3. [ WHERE <conditions> ]
  4. [YIELD [DISTINCT] <return_list>]
  5. [{SAMPLE <sample_list> | LIMIT <limit_list>}]
  6. [| GROUP BY {col_name | expr | position} YIELD <col_name>]
  7. [| ORDER BY <expression> [{ASC | DESC}]]
  8. [| LIMIT [<offset>,] <number_rows>];
  9. <vertex_list> ::=
  10. <vid> [, <vid> ...]
  11. <edge_type_list> ::=
  12. edge_type [, edge_type ...]
  13. | *
  14. <return_list> ::=
  15. <col_name> [AS <col_alias>] [, <col_name> [AS <col_alias>] ...]
  • <N> STEPS:指定跳数。如果没有指定跳数,默认值N1。如果N0,Nebula Graph不会检索任何边。

    Note

    GO语句采用的路径类型是walk,即遍历时点和边可以重复。详情参见路径

  • M TO N STEPS:遍历M~N跳的边。如果M0,输出结果和M1相同,即GO 0 TO 2GO 1 TO 2是相同的。

  • <vertex_list>:用逗号分隔的点ID列表,或特殊的引用符$-.id。详情参见管道符

  • <edge_type_list>:遍历的Edge type列表。

  • REVERSELY | BIDIRECT:默认情况下检索的是<vertex_list>的出边(正向),REVERSELY表示反向,即检索入边;BIDIRECT 为双向,即检索正向和反向,通过返回 <edge_type>._type 字段判断方向,其正数为正向,负数为反向。

  • WHERE <conditions>:指定遍历的过滤条件。用户可以在起始点、目的点和边使用WHERE子句,还可以结合ANDORNOTXOR一起使用。详情参见WHERE

    Note

    遍历多个Edge type时,WHERE子句有一些限制。例如不支持WHERE edge1.prop1 > edge2.prop2

  • YIELD [DISTINCT] <return_list>:定义需要返回的输出。<return_list>建议使用Schema函数,当前支持src(edge)dst(edge)type(edge)rank(edge)properties(edge)id(vertex)properties(vertex),暂不支持嵌套函数。详情参见YIELD。如果没有指定,默认返回目的点ID。

  • SAMPLE <sample_list>:用于在结果集中取样。详情参见SAMPLE

  • LIMIT <limit_list>:用于在遍历过程中逐步限制输出数量。详情参见LIMIT

  • GROUP BY:根据指定属性的值将输出分组。详情参见GROUP BY。分组后需要再次使用YIELD定义需要返回的输出。

  • ORDER BY:指定输出结果的排序规则。详情参见ORDER BY

    Note

    没有指定排序规则时,输出结果的顺序不是固定的。

  • LIMIT [<offset>,] <number_rows>]:限制输出结果的行数。详情参见LIMIT

示例

  1. # 返回player102所属队伍。
  2. nebula> GO FROM "player102" OVER serve;
  3. +------------+
  4. | serve._dst |
  5. +------------+
  6. | "team203" |
  7. | "team204" |
  8. +------------+
  1. # 返回距离player102两跳的朋友。
  2. nebula> GO 2 STEPS FROM "player102" OVER follow;
  3. +-------------+
  4. | follow._dst |
  5. +-------------+
  6. | "player101" |
  7. | "player125" |
  8. +-------------+
  9. ...
  1. # 添加过滤条件。
  2. nebula> GO FROM "player100", "player102" OVER serve \
  3. WHERE properties(edge).start_year > 1995 \
  4. YIELD DISTINCT properties($$).name AS team_name, properties(edge).start_year AS start_year, properties($^).name AS player_name;
  5. +-----------------+------------+---------------------+
  6. | team_name | start_year | player_name |
  7. +-----------------+------------+---------------------+
  8. | "Spurs" | 1997 | "Tim Duncan" |
  9. | "Trail Blazers" | 2006 | "LaMarcus Aldridge" |
  10. | "Spurs" | 2015 | "LaMarcus Aldridge" |
  11. +-----------------+------------+---------------------+
  1. # 遍历多个Edge type。属性没有值时,会显示UNKNOWN_PROP。
  2. nebula> GO FROM "player100" OVER follow, serve \
  3. YIELD properties(edge).degree, properties(edge).start_year;
  4. +-------------------------+-----------------------------+
  5. | properties(EDGE).degree | properties(EDGE).start_year |
  6. +-------------------------+-----------------------------+
  7. | 95 | UNKNOWN_PROP |
  8. | 95 | UNKNOWN_PROP |
  9. | UNKNOWN_PROP | 1997 |
  10. +-------------------------+-----------------------------+
  1. # 返回player100入方向的邻居点。
  2. nebula> GO FROM "player100" OVER follow REVERSELY \
  3. YIELD src(edge) AS destination;
  4. +-------------+
  5. | destination |
  6. +-------------+
  7. | "player101" |
  8. | "player102" |
  9. +-------------+
  10. ...
  11. # 该MATCH查询与上一个GO查询具有相同的语义。
  12. nebula> MATCH (v)<-[e:follow]- (v2) WHERE id(v) == 'player100' \
  13. RETURN id(v2) AS destination;
  14. +-------------+
  15. | destination |
  16. +-------------+
  17. | "player101" |
  18. | "player102" |
  19. +-------------+
  20. ...
  1. # 查询player100的朋友和朋友所属队伍。
  2. nebula> GO FROM "player100" OVER follow REVERSELY \
  3. YIELD src(edge) AS id | \
  4. GO FROM $-.id OVER serve \
  5. WHERE properties($^).age > 20 \
  6. YIELD properties($^).name AS FriendOf, properties($$).name AS Team;
  7. +---------------------+-----------------+
  8. | FriendOf | Team |
  9. +---------------------+-----------------+
  10. | "Boris Diaw" | "Spurs" |
  11. | "Boris Diaw" | "Jazz" |
  12. | "Boris Diaw" | "Suns" |
  13. ...
  14. # 该MATCH查询与上一个GO查询具有相同的语义。
  15. nebula> MATCH (v)<-[e:follow]- (v2)-[e2:serve]->(v3) \
  16. WHERE id(v) == 'player100' \
  17. RETURN v2.name AS FriendOf, v3.name AS Team;
  18. +---------------------+-----------------+
  19. | FriendOf | Team |
  20. +---------------------+-----------------+
  21. | "Boris Diaw" | "Spurs" |
  22. | "Boris Diaw" | "Jazz" |
  23. | "Boris Diaw" | "Suns" |
  24. ...
  1. # 查询player100 1~2跳内的朋友。
  2. nebula> GO 1 TO 2 STEPS FROM "player100" OVER follow \
  3. YIELD dst(edge) AS destination;
  4. +-------------+
  5. | destination |
  6. +-------------+
  7. | "player101" |
  8. | "player125" |
  9. ...
  10. # 该MATCH查询与上一个GO查询具有相同的语义。
  11. nebula> MATCH (v) -[e:follow*1..2]->(v2) \
  12. WHERE id(v) == "player100" \
  13. RETURN id(v2) AS destination;
  14. +-------------+
  15. | destination |
  16. +-------------+
  17. | "player100" |
  18. | "player102" |
  19. ...
  1. # 根据年龄分组。
  2. nebula> GO 2 STEPS FROM "player100" OVER follow \
  3. YIELD src(edge) AS src, dst(edge) AS dst, properties($$).age AS age \
  4. | GROUP BY $-.dst \
  5. YIELD $-.dst AS dst, collect_set($-.src) AS src, collect($-.age) AS age;
  6. +-------------+----------------------------+----------+
  7. | dst | src | age |
  8. +-------------+----------------------------+----------+
  9. | "player125" | ["player101"] | [41] |
  10. | "player100" | ["player125", "player101"] | [42, 42] |
  11. | "player102" | ["player101"] | [33] |
  12. +-------------+----------------------------+----------+
  1. # 分组并限制输出结果的行数。
  2. nebula> $a = GO FROM "player100" OVER follow YIELD src(edge) AS src, dst(edge) AS dst; \
  3. GO 2 STEPS FROM $a.dst OVER follow \
  4. YIELD $a.src AS src, $a.dst, src(edge), dst(edge) \
  5. | ORDER BY $-.src | OFFSET 1 LIMIT 2;
  6. +-------------+-------------+-------------+-------------+
  7. | src | $a.dst | follow._src | follow._dst |
  8. +-------------+-------------+-------------+-------------+
  9. | "player100" | "player125" | "player100" | "player101" |
  10. | "player100" | "player101" | "player100" | "player125" |
  11. +-------------+-------------+-------------+-------------+
  1. # 在多个边上通过IS NOT EMPTY进行判断。
  2. nebula> GO FROM "player100" OVER follow WHERE properties($$).name IS NOT EMPTY YIELD dst(edge);
  3. +-------------+
  4. | follow._dst |
  5. +-------------+
  6. | "player125" |
  7. | "player101" |
  8. +-------------+

最后更新: November 1, 2021