字符串运算符

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

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

示例

+

  1. nebula> RETURN 'a' + 'b';
  2. +-------+
  3. | (a+b) |
  4. +-------+
  5. | "ab" |
  6. +-------+
  7. nebula> UNWIND 'a' AS a UNWIND 'b' AS b RETURN a + b;
  8. +-------+
  9. | (a+b) |
  10. +-------+
  11. | "ab" |
  12. +-------+

CONTAINS

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

  1. nebula> MATCH (s:player)-[e:serve]->(t:team) WHERE id(s) == "player101" \
  2. AND t.name CONTAINS "ets" RETURN s.name, e.start_year, e.end_year, t.name;
  3. +---------------+--------------+------------+-----------+
  4. | s.name | e.start_year | e.end_year | t.name |
  5. +---------------+--------------+------------+-----------+
  6. | "Tony Parker" | 2018 | 2019 | "Hornets" |
  7. +---------------+--------------+------------+-----------+
  8. nebula> GO FROM "player101" OVER serve WHERE (STRING)serve.start_year CONTAINS "19" AND \
  9. $^.player.name CONTAINS "ny" \
  10. YIELD $^.player.name, serve.start_year, serve.end_year, $$.team.name;
  11. +----------------+------------------+----------------+--------------+
  12. | $^.player.name | serve.start_year | serve.end_year | $$.team.name |
  13. +----------------+------------------+----------------+--------------+
  14. | "Tony Parker" | 1999 | 2018 | "Spurs" |
  15. +----------------+------------------+----------------+--------------+
  16. nebula> GO FROM "player101" OVER serve WHERE !($$.team.name CONTAINS "ets") \
  17. YIELD $^.player.name, serve.start_year, serve.end_year, $$.team.name;
  18. +----------------+------------------+----------------+--------------+
  19. | $^.player.name | serve.start_year | serve.end_year | $$.team.name |
  20. +----------------+------------------+----------------+--------------+
  21. | "Tony Parker" | 1999 | 2018 | "Spurs" |
  22. +----------------+------------------+----------------+--------------+

(NOT) IN

  1. nebula> RETURN 1 IN [1,2,3], "Yao" NOT IN ["Yi", "Tim", "Kobe"], NULL in ["Yi", "Tim", "Kobe"]
  2. +----------------+------------------------------------+-------------------------------+
  3. | (1 IN [1,2,3]) | ("Yao" NOT IN ["Yi","Tim","Kobe"]) | (NULL IN ["Yi","Tim","Kobe"]) |
  4. +----------------+------------------------------------+-------------------------------+
  5. | true | true | false |
  6. +----------------+------------------------------------+-------------------------------+

(NOT) STARTS WITH

  1. nebula> RETURN 'apple' STARTS WITH 'app', 'apple' STARTS WITH 'a', 'apple' STARTS WITH toUpper('a')
  2. +-----------------------------+---------------------------+------------------------------------+
  3. | ("apple" STARTS WITH "app") | ("apple" STARTS WITH "a") | ("apple" STARTS WITH toUpper("a")) |
  4. +-----------------------------+---------------------------+------------------------------------+
  5. | true | true | false |
  6. +-----------------------------+---------------------------+------------------------------------+
  7. nebula> RETURN 'apple' STARTS WITH 'b','apple' NOT STARTS WITH 'app'
  8. +---------------------------+---------------------------------+
  9. | ("apple" STARTS WITH "b") | ("apple" NOT STARTS WITH "app") |
  10. +---------------------------+---------------------------------+
  11. | false | false |
  12. +---------------------------+---------------------------------+

(NOT) ENDS WITH

  1. nebula> RETURN 'apple' ENDS WITH 'app', 'apple' ENDS WITH 'e', 'apple' ENDS WITH 'E', 'apple' ENDS WITH 'b'
  2. +---------------------------+-------------------------+-------------------------+-------------------------+
  3. | ("apple" ENDS WITH "app") | ("apple" ENDS WITH "e") | ("apple" ENDS WITH "E") | ("apple" ENDS WITH "b") |
  4. +---------------------------+-------------------------+-------------------------+-------------------------+
  5. | false | true | false | false |
  6. +---------------------------+-------------------------+-------------------------+-------------------------+

正则表达式

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

  1. nebula> RETURN "384748.39" =~ "\\d+(\\.\\d{2})?";
  2. +----------------------------+
  3. | (384748.39=~\d+(\.\d{2})?) |
  4. +----------------------------+
  5. | true |
  6. +----------------------------+
  7. nebula> MATCH (v:player) WHERE v.name =~ 'Tony.*' RETURN v.name;
  8. +---------------+
  9. | v.name |
  10. +---------------+
  11. | "Tony Parker" |
  12. +---------------+