跳转至

字符串运算符Graph

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

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

Note

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

示例Graph

+Graph

nebula> RETURN 'a' + 'b';
+-------+
| (a+b) |
+-------+
| "ab"  |
+-------+
nebula> UNWIND 'a' AS a UNWIND 'b' AS b RETURN a + b;
+-------+
| (a+b) |
+-------+
| "ab"  |
+-------+

CONTAINSGraph

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

nebula> MATCH (s:player)-[e:serve]->(t:team) WHERE id(s) == "player101" \
        AND t.name CONTAINS "ets" RETURN s.name, e.start_year, e.end_year, t.name;
+---------------+--------------+------------+-----------+
| s.name        | e.start_year | e.end_year | t.name    |
+---------------+--------------+------------+-----------+
| "Tony Parker" | 2018         | 2019       | "Hornets" |
+---------------+--------------+------------+-----------+

nebula> GO FROM "player101" OVER serve WHERE (STRING)serve.start_year CONTAINS "19" AND \
        $^.player.name CONTAINS "ny" \
        YIELD $^.player.name, serve.start_year, serve.end_year, $$.team.name;
+----------------+------------------+----------------+--------------+
| $^.player.name | serve.start_year | serve.end_year | $$.team.name |
+----------------+------------------+----------------+--------------+
| "Tony Parker"  | 1999             | 2018           | "Spurs"      |
+----------------+------------------+----------------+--------------+

nebula> GO FROM "player101" OVER serve WHERE !($$.team.name CONTAINS "ets") \
        YIELD $^.player.name, serve.start_year, serve.end_year, $$.team.name;
+----------------+------------------+----------------+--------------+
| $^.player.name | serve.start_year | serve.end_year | $$.team.name |
+----------------+------------------+----------------+--------------+
| "Tony Parker"  | 1999             | 2018           | "Spurs"      |
+----------------+------------------+----------------+--------------+

(NOT) INGraph

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 WITHGraph

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 WITHGraph

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                   |
+---------------------------+-------------------------+-------------------------+-------------------------+

正则表达式Graph

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.name =~ 'Tony.*' RETURN v.name;
+---------------+
| v.name        |
+---------------+
| "Tony Parker" |
+---------------+

最后更新: August 19, 2021
Back to top