跳转至

FIND PATH

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

Note

用户可在配置文件nebula-graphd.conf中添加num_operator_threads参数提高FIND PATH的查询性能。num_operator_threads的取值为2~10,该值不能超过 Graph 服务所在机器的 CPU 核心个数,建议设置为 Graph 服务所在机器的 CPU 核心个数。关于配置文件的详细信息,参见 Graph 服务配置

语法

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 | BIDIRECTREVERSELY表示反向,BIDIRECT表示双向。
  • <WHERE clause>:可以使用WHERE子句过滤边属性。
  • UPTO <N> {STEP|STEPS}:路径的最大跳数。默认值为5
  • ORDER BY $-.path:将返回结果进行排序。排序规则参见 Path
  • LIMIT <M>:指定返回的最大行数。

Note

FIND PATH语句检索的路径类型为trail,即检索的路径只有点可以重复,边不可以重复。详情请参见路径

限制

  • 指定起始点和目的点的列表后,会返回起始点和目的点所有组合的路径。
  • 搜索所有路径时可能会出现循环。
  • 使用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")>                                                         |
| <("player130")-[:serve@0 {}]->("team219")<-[:serve@0 {}]-("player112")-[:serve@0 {}]->("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")> |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# 查找所有从 player100 到 team204,并且 degree 为空或者大于等于 0 的路径。
nebula> FIND ALL PATH FROM "player100" TO "team204" OVER * WHERE follow.degree is EMPTY or follow.degree >=0 YIELD path AS p;
+------------------------------------------------------------------------------+
| p                                                                            |
+------------------------------------------------------------------------------+
| <("player100")-[:serve@0 {}]->("team204")>                                   |
| <("player100")-[:follow@0 {}]->("player125")-[:serve@0 {}]->("team204")>     |
| <("player100")-[:follow@0 {}]->("player101")-[:serve@0 {}]->("team204")>     |
| ...                                                                          |
+------------------------------------------------------------------------------+
# 查找所有从 player100 到 team204 无环路径。
nebula> FIND NOLOOP PATH FROM "player100" TO "team204" OVER * YIELD path AS p;
+--------------------------------------------------------------------------------------------------------+
| p                                                                                                      |
+--------------------------------------------------------------------------------------------------------+
| <("player100")-[:serve@0 {}]->("team204")>                                                             |
| <("player100")-[:follow@0 {}]->("player125")-[:serve@0 {}]->("team204")>                               |
| <("player100")-[:follow@0 {}]->("player101")-[:serve@0 {}]->("team204")>                               |
| <("player100")-[:follow@0 {}]->("player101")-[:follow@0 {}]->("player125")-[:serve@0 {}]->("team204")> |
| <("player100")-[:follow@0 {}]->("player101")-[:follow@0 {}]->("player102")-[:serve@0 {}]->("team204")> |
| ...                                                                                                    |
+--------------------------------------------------------------------------------------------------------+

FAQ

是否支持 WHERE 子句,以实现图遍历过程中的条件过滤?

支持使用WHERE子句过滤,但只能过滤边属性,不支持过滤点属性。

如示例中的 WHERE follow.degree is EMPTY or follow.degree >= 0


最后更新: September 6, 2024