
# 支持的表达式，函数及过程
#### 表达式
Cypher查询支持多种的表达式，可以组合成丰富的过滤条件，目前支持的表达式如下：
| 运算类型                                       | 表达式                                                      | 举例\&备注                                                                                                                                                                                                                                                                                               |
|:---|:---|:---|
| 逻辑运算                                       | and                                                      | match (n:user) where n.age='Under 18' and n.gender='F' return n                                                                                                                                                                                                                                      |
| 逻辑运算                                       | or                                                       | match(n:user) where n.\`Zip-code\`='22181' or n.userid=6 return n                                                                                                                                                                                                                                    |
| 逻辑运算                                       | not                                                      | match(n:movie) where not n.genres contains 'Drama' return n                                                                                                                                                                                                                                          |
| 空值判断  | is null                                                  | match (n) where n.userid is null return n                                                                                                                                                                                                                                                            |
| 空值判断  | is not null                                              | match (n) where n.userid is not null return n                                                                                                                                                                                                                                                        |
| 比较运算                                       | \>,\>=,\<,\<=,=,\<\>                                     | match(n:user) where n.userid\>=5 return n                                                                                                                                                                                                                                                            |
| 算数运算(2.3.10)                               | +,-,\*,/,%,\^                                            | return (1+3)%3                                                                                                                                                                                                                                                                                       |
| 字符串比较                                      | starts with                                              | match(n:movie) where n.genres starts with 'Comedy' return n                                                                                                                                                                                                                                          |
| 字符串比较                                      | ends with                                                | match(n:movie) where n.genres ends with 'Drama' return n                                                                                                                                                                                                                                             |
| 字符串比较                                      | contains                                                 | match(n:movie) where n.genres contains 'Drama' return n                                                                                                                                                                                                                                              |
| List相关运算                                   | in                                                       | match(n:student) where 'math' in n.courses return n                                                                                                                                                                                                                                                  |
| List相关运算                                   | \[\]运算符                                                  | match(n:user) return n\[' userid'\] with \[1, 2, 3, 4\] as list return list\[0\] with \[1, 2, 3, 4\] as list return list\[0..1\] match p=(n)--\>(m) return \[x in nodes(p) where x.gender='F'\|id(x)\] |
| 日期表达式(2.3.10)                              | .year, .month, .day, .hour, .minute, .second, .dayOfWeek | 可以获取一个具体日期的年月日信息：with '2000-12-27 23:44:41' as strVal with datetime(strVal) as d2 return d2.year, d2.month, d2.day, d2.hour, d2.minute, d2.second,d2.dayOfWeek, d2.ordinalDay                                                                                                                        |
   
![](https://support.huaweicloud.com/api-ges/public_sys-resources/note_3.0-zh-cn.png)
Cypher查询的where子句暂不支持正则匹配。
#### 函数
在分组聚集、点边操作时，cypher支持一系列的函数，目前支持的函数如下所示：
![](https://support.huaweicloud.com/api-ges/public_sys-resources/note_3.0-zh-cn.png)
Cypher查询输入函数的大小写不敏感（即不区分大小写）。
1. 聚集函数
   目前支持count、collect两个聚集函数。
   
   | 函数名     | 支持的最低版本 | 释义       | 举例                                                                                       |
   |:---|:---|:---|:---|
   | count   | 2.2.17  | 求结果总数    | match (n) return count(\*) match (n) return count(n.userid) |
   | collect | 2.2.17  | 将结果聚集为列表 | match (n:movie) return n.genres, collect(n) as movieList                                 |
   | sum     | 2.3.3   | 结果求和     | unwind \[1, 2.0, 3\] as p return sum(p)                                                  |
   | avg     | 2.3.3   | 结果求均值    | unwind \[1, 2.0, 3\] as p return avg(p)                                                  |
   | min     | 2.3.3   | 求最小值     | unwind \[1, 2.0, 3\] as p return min(p)                                                  |
   | max     | 2.3.3   | 求最大值     | unwind \[1, 2.0, 3\] as p return max(p)                                                  |
      
   
2. 普通函数 根据入参不同，普通函数分为点边操作类、路径操作类、列表操作类、值操作类等几类函数。
   表1点边操作类 
   | 函数名       | 支持的最低版本 | 释义        | 举例                                                 |
   |:---|:---|:---|:---|
   | id        | 2.2.16  | 获取点的id    | match (n) return id(n)                             |
   | labels    | 2.2.16  | 获取点的label | match (n) return labels(n)                         |
   | type      | 2.2.16  | 获取边的label | match(n)-\[r\]-\>(m) return type(r)                |
   | degree    | 2.2.26  | 获取点的度数    | match (n) where id(n)='Vivian' return degree(n)    |
   | inDegree  | 2.2.26  | 获取点的入度    | match (n) where id(n)='Vivian' return inDegree(n)  |
   | outDegree | 2.2.26  | 获取点的出度    | match (n) where id(n)='Vivian' return outDegree(n) |
   | startNode | 2.3.10  | 获取边的入点    | match (n)-\[r\]-\>(m) return startNode(r)          |
   | endNode   | 2.3.10  | 获取边的出点    | match (n)-\[r\]-\>(m) return endNode(r)            |
      
   表2路径操作类函数 
   | 函数名           | 支持的最低版本 | 释义        | 举例                                                           |
   |:---|:---|:---|:---|
   | nodes         | 2.2.19  | 获取路径上的点列表 | match p=(n)-\[:friends\*1..2\]-\>(m) return nodes(p)         |
   | relationships | 2.2.19  | 获取路径上的边列表 | match p=(n)-\[:friends\*1..2\]-\>(m) return relationships(p) |
   | length        | 2.2.19  | 获取路径长度    | match p=(n)-\[:friends\*1..2\]-\>(m) return length(p)        |
      
   表3列表操作类函数 
   | 函数名   | 支持的最低版本 | 释义          | 举例                                         |
   |:---|:---|:---|:---|
   | head  | 2.3.10  | 获取列表的第一个元素  | with \[1,2,3,4\] as list return head(list) |
   | last  | 2.3.10  | 获取列表的最后一个元素 | with \[1,2,3,4\] as list return last(list) |
   | size  | 2.3.10  | 获取列表长度      | with \[1,2,3,4\] as list return size(list) |
   | range | 2.3.10  | 生成一个列表      | return range(1,5), range(1,5,2)            |
      
   表4值操作类 
   |    函数名    | 支持的最低版本 |       释义        |                              举例                              |
   |---|---|---|---|
   | toString  | 2.2.21  | 将其他值类型转换为string | match (n) where toString(labels(n)) contains 'movi' return n |
   | toUpper   | 2.2.26  | 将字符串变为大写        | match (n:movie) return toUpper(n.title)                      |
   | toLower   | 2.2.26  | 将字符串变为小写        | match (n:movie) return toLower(n.title)                      |
   | toInteger | 2.2.29  | 将字符串转为int类型     | with '123' as p return toInteger(p)                          |
   | toLong    | 2.2.29  | 将字符串转为long类型    | with '123' as p return toLong(p)                             |
   | toFloat   | 2.2.29  | 将字符串转为float类型   | with '123.4' as p return toFloat(p)                          |
   | toDouble  | 2.2.29  | 将字符串转为double类型  | with '123.4' as p return toDouble(p)                         |
   | toBoolean | 2.2.29  | 将字符串转为bool类型    | with 'true' as p return toBoolean(p)                         |
   | size      | 2.2.29  | 获取字符串的字符长度      | with 'GES' as p return size(p)                               |
   | subString | 2.3.10  | 截取字符串的一部分       | return subString('abc', 1), subString('abcde', 1,2)          |
   | coalesce  | 2.3.10  | 获取参数中第一个非null值。 | return coalesce(null, '123')                                 |
   | trim      | 2.3.11  | 移除字符串两侧的空白字符。   | return trim(' hello ')                                       |
   | lTrim     | 2.3.11  | 移除字符串左侧的空白字符。   | return lTrim(' hello')                                       |
   | rTrim     | 2.3.11  | 移除字符串右侧的空白字符。   | return rTrim('hello ')                                       |
   | reverse   | 2.3.11  | 翻转字符串           | return reverse('hello')                                      |
   | left      | 2.3.11  | 从字符串左侧取若干个字符    | with 'hello' as p return left(p, 3)                          |
   | right     | 2.3.11  | 从字符串右侧取若干个字符    | with 'hello' as p return right(p, 3)                         |
   | replace   | 2.3.11  | 字符串替换           | with 'hello' as p return replace(p, 'll', 'o')               |
   | split     | 2.3.11  | 字符串切割           | with 'hello' as p return split(p, 'e')                       |
      
   表5数学函数 
   | 函数名     | 支持的最低版本 | 释义       | 举例                            |
   |:---|:---|:---|:---|
   | floor   | 2.3.10  | 向下取整     | return floor(4.1)             |
   | ceil    | 2.3.10  | 向上取整     | return ceil(4.1)              |
   | round   | 2.3.14  | 取整       | return round(3.4), round(3.5) |
   | abs     | 2.3.14  | 绝对值函数    | return abs(-3),abs(-3.5)      |
   | sin     | 2.3.14  | 正弦函数     | return sin(pi()/2)            |
   | cos     | 2.3.14  | 余弦函数     | return cos(0),cos(pi()/2)     |
   | tan     | 2.3.14  | 正切函数     | return tan(pi()/4)            |
   | acos    | 2.3.14  | 反余弦函数    | return acos(1)                |
   | asin    | 2.3.14  | 反正弦函数    | return asin(0)                |
   | atan    | 2.3.14  | 反正切函数    | return atan(1)                |
   | cot     | 2.3.14  | 余切函数     | return cot(pi()/4)            |
   | radians | 2.3.14  | 度数转弧度    | return radians(180)           |
   | degrees | 2.3.14  | 弧度转度数    | return degrees(pi())          |
   | pi      | 2.3.14  | 返回圆周率近似值 | return pi()                   |
      
   表6日期时间函数 
   |      函数名       | 支持的最低版本 |        释义        |                   举例                    |
   |---|---|---|---|
   | datetime(val)  | 2.3.10  | 根据时间戳返回时间        | return datetime(1688696395)             |
   | datetime()     | 2.3.14  | 获取当前时间（仅读语句生效）   | return datetime()                       |
   | timestamp(val) | 2.3.10  | 根据时间字符串返回时间戳     | return timestamp('2023-07-07 02:20:42') |
   | timestamp()    | 2.3.14  | 获取当前时间戳（仅读语句生效）  | return timestamp()                      |
   | localDatetime  | 2.3.14  | 将时间/时间戳转为本地时间字符串 | return localDatetime(timestamp())       |
      
   表7谓词函数 
   | 函数名    | 支持的最低版本 | 释义                   | 举例                         |
   |:---|:---|:---|:---|
   | all    | 2.2.19  | 全部元素满足表达式，则返回true    | all (x in p where x\>1)    |
   | any    | 2.2.19  | 任意一个元素满足表达式，则返回true  | any (x in p where x\>1)    |
   | none   | 2.2.19  | 全部元素无法满足表达式，返回true   | none (x in p where x\>1)   |
   | single | 2.2.19  | 有且仅有1个元素满足表达式，返回true | single (x in p where x\>1) |
      
   表8算法表达式 
   | 函数名              | 支持的最低版本 | 释义                                            | 举例                                                                                                                        |
   |:---|:---|:---|:---|
   | shortestPath     | 2.3.2   | 返回两点间最短路  | 给定点n，m返回两点间最短路，方向为m到n，边label为rate： with n,m, shortestPath((n)\<-\[:rate\*\]-(m)) as p return p |
   | allShortestPaths | 2.3.2   | 返回两点间全最短路集合                                   | 给定点n，m返回两点间全最短路集合： with n,m, allShortestPaths((n)-\[\*\]-(m)) as p return p                   |
      
   
 
#### 过程
目前GES 支持如下过程（Procedure）：
| 名称                    | 语句                                          |
|:---|:---|
| 获取图模式相关信息             | call db.schema()                            |
| 获取点label              | call db.labels()                            |
| 获取边label              | call db.relationshipTypes()                 |
| 查询当前正在执行的Cypher语句     | call dbms.listQueries()                     |
| 根据queryId终止某条Cypher语句 | call dbms.killQuery('queryId')              |
| 查询索引                  | call db.indexes()                           |
| 全文索引，查询符合要求的点         | call db.index.fulltext.queryNodes()         |
| 全文索引，查询符合要求的边         | call db.index.fulltext.queryRelationships() |
   
![](https://support.huaweicloud.com/api-ges/public_sys-resources/note_3.0-zh-cn.png)
全文索引支持prefix（前缀）、wildcard（通配符）、regexp（正则）、fuzzy（模糊）、match（匹配）、combine（组合）6种查询，如果想使用全文索引的能力，需要预先调用创建全文索引的API。
![](https://support.huaweicloud.com/api-ges/public_sys-resources/note_3.0-zh-cn.png)
函数和过程名大小写敏感，须按小驼峰写法调用。
- 全文索引查询请求示例：
  ```
  POST http://{SERVER_URL}/ges/v1.0/{project_id}/graphs/{graph_name}/action?action_id=execute-cypher-query
  {
      "statements": [
          {
              "statement": "call db.index.fulltext.queryNodes('combine', {title:'1977'}) yield node, score return node, score skip 1 limit 10",
              "resultDataContents": [
                  "row"
              ],
              "parameters": {}
          }
      ]
   }
  ```
  

- 添加边时的平行边处理策略： 通过cypher添加边的时候，允许添加重复边，此处的重复边的定义为\<源点，终点\>相同的两条边。
  
- 添加无label的边的方法： 通过Cypher添加边时必须指定label，所以指定待添加边的label为默认值"__DEFAULT__"即可，例如create ()-\[r:__DEFAULT__\]-\>() return r
  
 
#### 通过Cypher查询Schema结构
- 功能介绍 查询生成的schema结构。
  
- 查询语句介绍
  - 名称：查询Schema结构。
  
  - 命令：call db.schema()
  
  - 说明： 当未执行生成Schema结构API时，返回schema文件中的所有label。
    当已执行生成Schema结构API时，以label为点，以label和label间关系为边返回。
    
   
 
