【NebulaGraph】官方查询语言nGQL教程1 (四)

【NebulaGraph】官方查询语言nGQL教程1

  • 1. 课程信息
  • 2. 查找路径`FIND PATH`
    • 2.1 补充说明`FIND PATH`
    • 2.2 例子

1. 课程信息

课程地址: https://www.bilibili.com/video/BV1PT411P7w8/?spm_id_from=333.337.search-card.all.click&vd_source=240d9002f7c7e3da63cd9a975639409a

2. 查找路径FIND PATH

FIND PATH语句查找指定起始点和目的点之间的路径。
语法:

FIND { SHORTEST | SINGLE SHORTEST | ALL | NOLOOP } PATH [WITH PROP] FROM <vertex_id_list> TO <vertex_id_list>
OVER <edge_type_list> [REVERSELY | BIDIRECT] 
[<WHERE clause>] [UPTO <N> {STEP|STEPS}] 
YIELD path as <alias>
[| ORDER BY $-.path] [| LIMIT <M>];

<vertex_id_list> ::=
    [vertex_id [, vertex_id] ...]
  • SHORTEST:查找所有最短路径。
  • SINGLE SHORTEST:查找所有最短路径,随机返回其中一条。
  • ALL:查找所有路径。
  • NOLOOP:查找非循环路径。
  • WITH PROP:展示点和边的属性。不添加本参数则隐藏属性。
  • <vertex_id_list>:点 ID 列表。多个点用英文逗号(,)分隔。支持 − 和 -和 var。
  • <edge_type_list>:Edge type 列表。多个 Edge type 用英文逗号(,)分隔。*表示所有 Edge type。
  • REVERSELY | BIDIRECT:REVERSELY表示反向,BIDIRECT表示双向。
  • <WHERE clause>:可以使用WHERE子句过滤边属性。
  • UPTO <N> {STEP|STEPS}:路径的最大跳数。默认值为5。
  • ORDER BY $-.path:将返回结果进行排序。排序规则参见 Path。
  • LIMIT <M>:指定返回的最大行数。

限制

  • 指定起始点和目的点的列表后,会返回起始点和目的点所有组合的路径。
  • 搜索所有路径时可能会出现循环。
  • 使用WHERE子句时只能过滤边属性,暂不支持过滤点属性,且不支持函数。
  • graphd 是单进程查询,会占用很多内存。

示例
返回的路径格式类似于

(<vertex_id>)-[:<edge_type_name>@<rank>]->(<vertex_id)
# 查找并返回 player102 到 team204 的最短路径。
nebula> FIND SHORTEST PATH FROM "player102" TO "team204" OVER * YIELD path AS p;
+--------------------------------------------+
| p                                          |
+--------------------------------------------+
| <("player102")-[:serve@0 {}]->("team204")> |
+--------------------------------------------+
# 查找并返回带属性值的 team204 到 player100 的最短反向路径。
nebula> FIND SHORTEST PATH WITH PROP FROM "team204" TO "player100" OVER * REVERSELY YIELD path AS p;
+--------------------------------------------------------------------------------------------------------------------------------------+
| p                                                                                                                                    |
+--------------------------------------------------------------------------------------------------------------------------------------+
| <("team204" :team{name: "Spurs"})<-[:serve@0 {end_year: 2016, start_year: 1997}]-("player100" :player{age: 42, name: "Tim Duncan"})> |
+--------------------------------------------------------------------------------------------------------------------------------------+
# 查找并返回起点为 player100,player130 而终点为 player132,player133 的 18 跳之内双向最短路径。
nebula> FIND SHORTEST PATH FROM "player100", "player130" TO "player132", "player133" OVER * BIDIRECT UPTO 18 STEPS YIELD path as p;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| p                                                                                                                                                                                              |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| <("player100")<-[:follow@0 {}]-("player144")<-[:follow@0 {}]-("player133")>                                                                                                                    |
| <("player100")-[:serve@0 {}]->("team204")<-[:serve@0 {}]-("player138")-[:serve@0 {}]->("team225")<-[:serve@0 {}]-("player132")>                                                                |
| <("player130")-[:serve@0 {}]->("team219")<-[:serve@0 {}]-("player112")-[:serve@0 {}]->("team204")<-[:serve@0 {}]-("player114")<-[:follow@0 {}]-("player133")>                                  |
| <("player130")-[:serve@0 {}]->("team219")<-[:serve@0 {}]-("player109")-[:serve@0 {}]->("team204")<-[:serve@0 {}]-("player114")<-[:follow@0 {}]-("player133")>                                  |
| <("player130")-[:serve@0 {}]->("team219")<-[:serve@0 {}]-("player104")-[:serve@20182019 {}]->("team204")<-[:serve@0 {}]-("player114")<-[:follow@0 {}]-("player133")>                           |
| ...                                                                                                                                                                                            |
| <("player130")-[:serve@0 {}]->("team219")<-[:serve@0 {}]-("player112")-[:serve@0 {}]->("team204")<-[:serve@0 {}]-("player138")-[:serve@0 {}]->("team225")<-[:serve@0 {}]-("player132")>        |
| <("player130")-[:serve@0 {}]->("team219")<-[:serve@0 {}]-("player109")-[:serve@0 {}]->("team204")<-[:serve@0 {}]-("player138")-[:serve@0 {}]->("team225")<-[:serve@0 {}]-("player132")>        |
| ...                                                                                                                                                                                            |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# 查找 player100 和 player130 分别与 player132 和 player133 之间跳数最长为 18 步的最短路径。
nebula> FIND SINGLE SHORTEST PATH FROM "player100", "player130" TO "player132", "player133" OVER * BIDIRECT UPTO 18 STEPS YIELD path as p;
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| p                                                                                                                                                                                       |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| <("player100")<-[:follow@0 {}]-("player144")<-[:follow@0 {}]-("player133")>                                                                                                             |
| <("player100")-[:serve@0 {}]->("team204")<-[:serve@0 {}]-("player138")-[:serve@0 {}]->("team225")<-[:serve@0 {}]-("player132")
上一篇:【人工智能】从基础到实践:用Python和PyTorch实现深度学习图像分割模型


下一篇:粉丝生产力与开源 AI 智能名片 2+1 链动模式商城小程序的融合创新与价值拓展