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

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

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

  • <edge_type_list>:遍历的边类型列表。

  • REVERSELY | BIDIRECT:默认情况下检索的是<vertex_list>的出边,REVERSELY表示反向,即检索入边,BIDIRECT表示双向,即检索出边和入边。

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

    说明:遍历多个边类型时,WHERE子句有一些限制。例如不支持WHERE edge1.prop1 > edge2.prop2

  • YIELD [DISTINCT] <return_list>:指定输出结果。详情请参见YIELD。如果没有指定,默认返回目的点ID。

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

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

  • LIMIT:限制输出结果的行数。详情请参见LIMIT

  • GROUP BY:根据指定属性的值将输出分组。详情请参见GROUP BY

示例

  1. # 返回player102所属队伍。
  2. nebula> GO FROM "player102" OVER serve;
  3. +------------+
  4. | serve._dst |
  5. +------------+
  6. | "team203" |
  7. +------------+
  8. | "team204" |
  9. +------------+
  1. # 返回距离player102两跳的朋友。
  2. nebula> GO 2 STEPS FROM "player102" OVER follow;
  3. +-------------+
  4. | follow._dst |
  5. +-------------+
  6. | "player101" |
  7. +-------------+
  8. | "player125" |
  9. +-------------+
  10. ...
  1. # 添加过滤条件。
  2. nebula> GO FROM "player100", "player102" OVER serve \
  3. WHERE serve.start_year > 1995 \
  4. YIELD DISTINCT $$.team.name AS team_name, serve.start_year AS start_year, $^.player.name AS player_name;
  5. +-----------------+------------+---------------------+
  6. | team_name | start_year | player_name |
  7. +-----------------+------------+---------------------+
  8. | "Spurs" | 1997 | "Tim Duncan" |
  9. +-----------------+------------+---------------------+
  10. | "Trail Blazers" | 2006 | "LaMarcus Aldridge" |
  11. +-----------------+------------+---------------------+
  12. | "Spurs" | 2015 | "LaMarcus Aldridge" |
  13. +-----------------+------------+---------------------+
  1. # 遍历多个边类型。属性没有值时,会显示__EMPTY__。
  2. nebula> GO FROM "player100" OVER follow, serve \
  3. YIELD follow.degree, serve.start_year;
  4. +---------------+------------------+
  5. | follow.degree | serve.start_year |
  6. +---------------+------------------+
  7. | 95 | __EMPTY__ |
  8. +---------------+------------------+
  9. | 95 | __EMPTY__ |
  10. +---------------+------------------+
  11. | __EMPTY__ | 1997 |
  12. +---------------+------------------+
  1. # 返回player100的入边。
  2. nebula> GO FROM "player100" OVER follow REVERSELY \
  3. YIELD follow._dst AS destination;
  4. +-------------+
  5. | destination |
  6. +-------------+
  7. | "player101" |
  8. +-------------+
  9. | "player102" |
  10. +-------------+
  11. ...
  12. # 该MATCH查询与上一个GO查询具有相同的语义。
  13. nebula> MATCH (v)<-[e:follow]- (v2) WHERE id(v) == 'player100' \
  14. RETURN id(v2) AS destination;
  15. +-------------+
  16. | destination |
  17. +-------------+
  18. | "player101" |
  19. +-------------+
  20. | "player102" |
  21. +-------------+
  22. ...
  1. # 查询player100的朋友和朋友所属队伍。
  2. nebula> GO FROM "player100" OVER follow REVERSELY \
  3. YIELD follow._dst AS id | \
  4. GO FROM $-.id OVER serve \
  5. WHERE $^.player.age > 20 \
  6. YIELD $^.player.name AS FriendOf, $$.team.name AS Team;
  7. +---------------------+-----------------+
  8. | FriendOf | Team |
  9. +---------------------+-----------------+
  10. | "Tony Parker" | "Spurs" |
  11. +---------------------+-----------------+
  12. | "Tony Parker" | "Hornets" |
  13. +---------------------+-----------------+
  14. ...
  15. # 该MATCH查询与上一个GO查询具有相同的语义。
  16. nebula> MATCH (v)<-[e:follow]- (v2)-[e2:serve]->(v3) \
  17. WHERE id(v) == 'player100' \
  18. RETURN v2.name AS FriendOf, v3.name AS Team;
  19. +---------------------+-----------------+
  20. | FriendOf | Team |
  21. +---------------------+-----------------+
  22. | "Tony Parker" | "Spurs" |
  23. +---------------------+-----------------+
  24. | "Tony Parker" | "Hornets" |
  25. +---------------------+-----------------+
  26. ...
  1. # 返回player102的出边和入边。
  2. nebula> GO FROM "player102" OVER follow BIDIRECT \
  3. YIELD follow._dst AS both;
  4. +-------------+
  5. | both |
  6. +-------------+
  7. | "player100" |
  8. +-------------+
  9. | "player101" |
  10. +-------------+
  11. ...
  12. # 该MATCH查询与上一个GO查询具有相同的语义。
  13. nebula> MATCH (v) -[e:follow]-(v2) \
  14. WHERE id(v)== "player102" \
  15. RETURN id(v2) AS both;
  16. +-------------+
  17. | both |
  18. +-------------+
  19. | "player101" |
  20. +-------------+
  21. | "player103" |
  22. +-------------+
  23. ...
  1. # 查询player100 1~2跳内的朋友。
  2. nebula> GO 1 TO 2 STEPS FROM "player100" OVER follow \
  3. YIELD follow._dst AS destination;
  4. +-------------+
  5. | destination |
  6. +-------------+
  7. | "player101" |
  8. +-------------+
  9. | "player125" |
  10. +-------------+
  11. ...
  12. # 该MATCH查询与上一个GO查询具有相同的语义。
  13. nebula> MATCH (v) -[e:follow*1..2]->(v2) \
  14. WHERE id(v) == "player100" \
  15. RETURN id(v2) AS destination;
  16. +-------------+
  17. | destination |
  18. +-------------+
  19. | "player100" |
  20. +-------------+
  21. | "player102" |
  22. +-------------+
  1. # 根据年龄分组。
  2. nebula> GO 2 STEPS FROM "player100" OVER follow \
  3. YIELD follow._src AS src, follow._dst AS dst, $$.player.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. +-------------+----------------------------+----------+
  11. | "player100" | ["player125", "player101"] | [42, 42] |
  12. +-------------+----------------------------+----------+
  13. | "player102" | ["player101"] | [33] |
  14. +-------------+----------------------------+----------+
  1. # 分组并限制输出结果的行数。
  2. nebula> $a = GO FROM "player100" OVER follow YIELD follow._src AS src, follow._dst AS dst; \
  3. GO 2 STEPS FROM $a.dst OVER follow \
  4. YIELD $a.src AS src, $a.dst, follow._src, follow._dst \
  5. | ORDER BY $-.src | OFFSET 1 LIMIT 2;
  6. +-------------+-------------+-------------+-------------+
  7. | src | $a.dst | follow._src | follow._dst |
  8. +-------------+-------------+-------------+-------------+
  9. | "player100" | "player125" | "player100" | "player101" |
  10. +-------------+-------------+-------------+-------------+
  11. | "player100" | "player101" | "player100" | "player125" |
  12. +-------------+-------------+-------------+-------------+