跳转至

字符串运算符

NebulaGraph 支持使用字符串运算符进行连接、搜索、匹配运算。支持的运算符如下。

名称 说明
+ 连接字符串。
CONTAINS 在字符串中执行搜索。
(NOT) IN 字符串是否匹配某个值。
(NOT) STARTS WITH 在字符串的开头执行匹配。
(NOT) ENDS WITH 在字符串的结尾执行匹配。
正则表达式 通过正则表达式匹配字符串。

Note

所有搜索或匹配都区分大小写。

示例

+

# 返回连接后的字符串。
nebula> RETURN 'a' + 'b';
+-----------+
| ("a"+"b") |
+-----------+
| "ab"      |
+-----------+
nebula> UNWIND 'a' AS a UNWIND 'b' AS b RETURN a + b;
+-------+
| (a+b) |
+-------+
| "ab"  |
+-------+

CONTAINS

CONTAINS要求待运算的左右两边都是字符串类型。

# 查询返回 id 值为 player101,且目标点属性 name 中含有"ets"的数据。
nebula> MATCH (s:player)-[e:serve]->(t:team) WHERE id(s) == "player101" \
        AND t.team.name CONTAINS "ets" RETURN s.player.name, e.start_year, e.end_year, t.team.name;
+---------------+--------------+------------+-------------+
| s.player.name | e.start_year | e.end_year | t.team.name |
+---------------+--------------+------------+-------------+
| "Tony Parker" | 2018         | 2019       | "Hornets"   |
+---------------+--------------+------------+-------------+

# 查询返回 player101 的边属性 start_year 转为字符串后含有"19",且点属性 name 中含有"ny"的数据。
nebula> GO FROM "player101" OVER serve WHERE (STRING)properties(edge).start_year CONTAINS "19" AND \
        properties($^).name CONTAINS "ny" \
        YIELD properties($^).name, properties(edge).start_year, properties(edge).end_year, properties($$).name;
+---------------------+-----------------------------+---------------------------+---------------------+
| properties($^).name | properties(EDGE).start_year | properties(EDGE).end_year | properties($$).name |
+---------------------+-----------------------------+---------------------------+---------------------+
| "Tony Parker"       | 1999                        | 2018                      | "Spurs"             |
+---------------------+-----------------------------+---------------------------+---------------------+

# 查询返回 palyer101 指向的目标点属性 name 中不含有"ets"的数据。
nebula> GO FROM "player101" OVER serve WHERE !(properties($$).name CONTAINS "ets") \
        YIELD properties($^).name, properties(edge).start_year, properties(edge).end_year, properties($$).name;
+---------------------+-----------------------------+---------------------------+---------------------+
| properties($^).name | properties(EDGE).start_year | properties(EDGE).end_year | properties($$).name |
+---------------------+-----------------------------+---------------------------+---------------------+
| "Tony Parker"       | 1999                        | 2018                      | "Spurs"             |
+---------------------+-----------------------------+---------------------------+---------------------+

(NOT) IN

# 返回列表中是否含有某个值。
nebula> RETURN  1 IN [1,2,3], "Yao" NOT IN ["Yi", "Tim", "Kobe"], NULL IN ["Yi", "Tim", "Kobe"];
+----------------+------------------------------------+-------------------------------+
| (1 IN [1,2,3]) | ("Yao" NOT IN ["Yi","Tim","Kobe"]) | (NULL IN ["Yi","Tim","Kobe"]) |
+----------------+------------------------------------+-------------------------------+
| true           | true                               | __NULL__                      |
+----------------+------------------------------------+-------------------------------+

(NOT) STARTS WITH

# 返回是否以某个字符串开头。
nebula> RETURN 'apple' STARTS WITH 'app', 'apple' STARTS WITH 'a', 'apple' STARTS WITH toUpper('a');
+-----------------------------+---------------------------+------------------------------------+
| ("apple" STARTS WITH "app") | ("apple" STARTS WITH "a") | ("apple" STARTS WITH toUpper("a")) |
+-----------------------------+---------------------------+------------------------------------+
| true                        | true                      | false                              |
+-----------------------------+---------------------------+------------------------------------+

nebula> RETURN 'apple' STARTS WITH 'b','apple' NOT STARTS WITH 'app';
+---------------------------+---------------------------------+
| ("apple" STARTS WITH "b") | ("apple" NOT STARTS WITH "app") |
+---------------------------+---------------------------------+
| false                     | false                           |
+---------------------------+---------------------------------+

(NOT) ENDS WITH

# 返回是否以某个字符串结尾。
nebula> RETURN 'apple' ENDS WITH 'app', 'apple' ENDS WITH 'e', 'apple' ENDS WITH 'E', 'apple' ENDS WITH 'b';
+---------------------------+-------------------------+-------------------------+-------------------------+
| ("apple" ENDS WITH "app") | ("apple" ENDS WITH "e") | ("apple" ENDS WITH "E") | ("apple" ENDS WITH "b") |
+---------------------------+-------------------------+-------------------------+-------------------------+
| false                     | true                    | false                   | false                   |
+---------------------------+-------------------------+-------------------------+-------------------------+

正则表达式

Note

当前仅 opencypher 兼容语句(MATCHWITH等)支持正则表达式,原生 nGQL 语句(FETCHGOLOOKUP等)不支持正则表达式。

NebulaGraph 支持使用正则表达式进行过滤,正则表达式的语法是继承自std::regex,用户可以使用语法=~ '<regexp>'进行正则表达式匹配。例如:

# 返回是否匹配正则表达式。
nebula> RETURN "384748.39" =~ "\\d+(\\.\\d{2})?";
+--------------------------------+
| ("384748.39"=~"\d+(\.\d{2})?") |
+--------------------------------+
| true                           |
+--------------------------------+

# 查询返回符合正则表达式的点数据。
nebula> MATCH (v:player) WHERE v.player.name =~ 'Tony.*' RETURN v.player.name;
+---------------+
| v.player.name |
+---------------+
| "Tony Parker" |
+---------------+

最后更新: 2024年9月14日