跳转至

OPTIONAL MATCH

Caution

目前 OPTIONAL MATCH 为 Beta 功能,后续可能会有一定优化调整。

OPTIONAL MATCH通常与MATCH语句一起使用,作为MATCH语句的可选项去匹配命中的模式,如果没有命中对应的模式,对应的列返回NULL

openCypher 兼容性

本文操作仅适用于 nGQL 中的 openCypher 方式。

使用限制

OPTIONAL MATCH子句中暂不支持使用WHERE子句。

示例

MATCH语句中使用OPTIONAL MATCH的示例如下:

# 找出特定节点 m(ID为 "player100" 的节点)直接连接的节点,以及这些节点 n 直连的下一层节点 l (OPTIONAL MATCH (n)-[]->(l)), 如果没有找到这样的节点 l,查询也会继续, l 的返回值是 null。
nebula> MATCH (m)-[]->(n) WHERE id(m)=="player100" \
        OPTIONAL MATCH (n)-[]->(l) \
        RETURN id(m),id(n),id(l);
+-------------+-------------+-------------+
| id(m)       | id(n)       | id(l)       |
+-------------+-------------+-------------+
| "player100" | "team204"   | __NULL__    |
| "player100" | "player101" | "team204"   |
| "player100" | "player101" | "team215"   |
| "player100" | "player101" | "player100" |
| "player100" | "player101" | "player102" |
| "player100" | "player101" | "player125" |
| "player100" | "player125" | "team204"   |
| "player100" | "player125" | "player100" |
+-------------+-------------+-------------+

而使用多MATCH,不使用OPTIONAL MATCH时,会返回模式完全匹配的行。示例如下:

# 找出特定节点 m(ID为 "player100" 的节点)直接连接的节点,以及这些节点 n 直连的下一层节点 l, 节点 l 必须存在。
nebula> MATCH (m)-[]->(n) WHERE id(m)=="player100" \
        MATCH (n)-[]->(l) \
        RETURN id(m),id(n),id(l);
+-------------+-------------+-------------+
| id(m)       | id(n)       | id(l)       |
+-------------+-------------+-------------+
| "player100" | "player101" | "team204"   |
| "player100" | "player101" | "team215"   |
| "player100" | "player101" | "player100" |
| "player100" | "player101" | "player102" |
| "player100" | "player101" | "player125" |
| "player100" | "player125" | "team204"   |
| "player100" | "player125" | "player100" |
+-------------+-------------+-------------+

最后更新: April 15, 2024