
Neo4j系列导航:
 neo4j及简单实践
 cypher语法基础
 cypher插入语法
 cypher插入语法
 cypher查询语法
 cypher通用语法
 cypher函数语法
4.常用函数
主要包括谓词函数(断言函数)、标量函数、聚合函数、字符串函数以及集合函数
4.1.谓词函数(断言函数)
谓词函数返回true或者false,主要用于检查是否存在或满足特定的条件。
4.1.1.exist()函数
判断是否存在某个属性或者模式
- 检查节点是否存在name属性 match (n) where exists(n.name) return n.name as name
- 检查是否存在Friend的关系 match (n) where exists()-[r:Friend]->()) return r
4.1.2.isEmpty()函数
检查元素是否为空,可在string,list和map类型中使用
- 检查string类型是否为空 match (n) where isEmpty(n.name) return n
- 检查list类型是否为空 with [1,2] as list return isEmpty(list)
- 检查map类型是否为空 match (n) with n {.name, .age} as map return isEmpty(map)
4.1.2.all()函数
检查集合元素,且所有的元素都满足条件 all(variable in list where predicate)
- 检查列表中的元素是否都大于3: with [1,2,3,4,5] as list return all(x in list where x>3)
- 检查在路径中,所有节点都必须具有age属性,并且age属性值都必须大于30: match p=(n)-[*1..3]->(m) where a=id(1) and all(x in nodes(p) where x.age > 30) return p
4.1.3.any()函数
检查集合元素,且至少一个元素满足条件 any(variable in list where predicate)
- 检查列表中的元素是否存在大于3: with [1,2,3,4,5] as list return any(x in list where x>3)
- 检查在路径中,是否存在节点具有age属性,并且age属性值大于30: match p=(n)-[*1..3]->(m) where id(n)=3 and any(x in nodes(p) where x.age > 30) return p
4.1.4.none()函数
检查集合元素,且所有的元素都不满足条件 nono(variable in list where predicate)
- 检查列表中的元素是否都不大于(<=)3: with [1,2,3,4,5] as list return none(x in list where x>3)
- 检查在路径中,所有节点的的age属性是否都不大于30: match p=(n)-[*1..3]->(m) whereid(n)=3 and none(x in nodes(p) where x.age > 30) return p
4.1.5.single()函数
检查集合元素,且只有一个元素满足条件 single(variable in list where predicate)
- 检查列表中的元素是否只有一个元素大于3: with [1,2,3,4,5] as list return none(x in list where x>3)
- 检查在路径中,是否是只有一个节点的age属性大于30: match p=(n)-[*1..3]->(m) where id(n)=3 and none(x in nodes(p) where x.age > 30) return p
注意:return single(x in [6,null] where x>2) 返回null
4.2.标量函数
4.2.1.id()函数
返回节点或者关系的id id(expression)
- 根据id过滤节点: match(n) where id(n)=3 return n
- 根据id过滤关系: match ()-[r]->() where id(r)=3 return r
- 返回节点和关系id: match (n)-[r]->() return id(n) as node_id, id(r) as edge_id
4.2.2.labels()函数
返回节点的Label,labels(null)返回null
- 返回节点label: 结果为list,因为节点可以有多个label match(n:Person{name:"zhangsan"})-[r]-(m) return labels(m)
4.2.3.type()、startNode()和endNode()函数
都是关系相关的函数
- type(): 返回关系的类型(Label) MATCH (n)-[r]->() where n.name="zhangsan" return type(r)
- startNode(): 返回关系的开始节点 MATCH (n)-[r]->() where n.name="zhangsan" return startNode(r)
- endNode(): 返回关系的结束节点(Label) MATCH (n)-[r]->() where n.name="zhangsan" return endNode(r)
4.2.4.properties()函数
返回节点或关系的属性(Map)
- 返回节点和关系properties: match (n)-[r]->() return properties(n) as node_properties, properties(r) as edge_properties
4.2.5.size()和length()函数
求长度的函数
- size(string): 求字符串中字符的数量(可以把字符串当作是字符的列表) return size("zhangsan")
- size(list): 返回列表中元素的数量 with [1,2,3,4,5] as list return size(list)
- size(pattern_expression):返回提供模式表达式中匹配到的数量 match (n) where a.name="zhangsan" return size((n)-->()) as path_size
 用于在匹配查询(Match query)中提供一组新的结果,这些结果是路径列表,size()函数用于统计路径列表中元素(即路径)的数量。
- length(path): 返回路径的长度,即路径中关系的数量 match p=(n:Person)-->() return length(p)
4.2.6.coalesce()函数
返回第一个非null值,如果都为null则返回null
- 返回第一个非null属性: match (n) return coalesce(n.created, n.address)
- 返回列表第一个非null元素: n.array是个列表 match (n) where id(n)=3 return n.array coalesce(n.array)
4.2.7.head()和last()函数
列表的函数
- 返回列表第一个元素: match (n) where id(n)=3 return n.array head(n.array)
- 返回列表最后一个元素: match (n) where id(n)=3 return n.array last(n.array)
4.2.8.类型转换函数
函数转换失败时,都返回null,不报异常,但是如果参数类型错误会报异常
-  toBoolean(): 转换为boolean类型, 可转换类型为string、boolean和integer return toBoolean("TRUE"), toBoolean("False")//返回true和false
-  toBooleanOrNull(): 将string、integer或布尔值转换为布尔值。对于任何其他输入值,将返回null。 return toBooleanOrNull('true'), toBooleanOrNull('not a boolean'), toBooleanOrNull(0), toBooleanOrNull(1.5)返回结果: true <null> false <null>
-  toFloat(): 转换为float类型, 可转换类型为number、string、boolean return toFloat(1), toFloat("1")
-  toFloatOrNull(): 将integer、float或string值转换为float。对于任何其他输入值,将返回null。 return toFloatOrNull('11.5'), toFloatOrNull('not a number'), toFloatOrNull(true)返回值: 11.5 <null> <null>
-  toInteger(): 转换为integer类型, 可转换类型为number、string、boolean return toInteger('42'), toInteger('not a number'), toInteger(true)//返回42 <null> 1
-  toIntegerOrNull(): 将integer、float或string值转换为integer。对于任何其他输入值,将返回null。 return toIntegerOrNull('42'), toIntegerOrNull('not a number'), toIntegerOrNull(true), toIntegerOrNull(['A', 'B', 'C'])返回值: 42 <null> 1 <null>
4.2.9.randomUUID()、timestamp()函数
- randomUUID(): 返回一个128位的唯一uuid return randomUUID()
- timestamp(): 返回当前时间(与1970年1月1日之间的毫秒值) return timestamp()
4.3.aggregation(聚合)函数
聚合函数用于对查询的结果进行统计,主要分为:
| 函数 | 含义 | 
|---|---|
| count(exp) | 计算值或记录的总数量,包括null值 | 
| sum() | 统计求和 | 
| avg() | 求平均数 | 
| min() | 统计求最小值 | 
| max() | 统计求最大值 | 
| collect() | 所有的值收集起来放入一个列表,空值null将被忽略 | 
| distinct() | 去重 | 
| percentileDisc() | 计算百分位 | 
| percentileCont() | 计算加权平均数 | 
| stdev() | 计算标准偏差(部分样本) | 
| stdep | 计算标准差(整个样本) | 
更加详细的解释和实例请看博主的另一篇文章:cypher查询语法中的2.5章节。
4.4.字符串函数
4.4.1.left、right和substring函数
从左边、右边或指定位置截取length长度字符
- 从左边截取三个: left(original,length)return left("hello", 3)//返回 “hel”
- 从右边截取三个: right(original,length)return right("hello", 3)//返回 “llo”
- 从给定位置截取length长度: substring(original, start, length)return substring('hello', 1, 3)//返回"ell"
- 从给定位置截取到末尾: substring(original, start)length不填截取到末尾return substring('hello', 2)//返回"llo"
4.4.2.ltrim、rtrim和trim函数
去空格
- 去掉前导(开始的)空格 ltrim(original)return ltrim(' hello')//返回 “hello”
- 去掉尾部空格 rtrim(original)return rtrim('hello ')//返回 “hello”
- 去掉前导和尾部空格 rtrim(original)return trim(' hello ')//返回 “hello”
4.4.3.replace函数
字符替换 replace(original, search, replace)
- 将hello中的I全替换为W: return replace("hello", "l", "w")//返回"hewwo"
4.4.4.reverse函数
颠倒字符转中字符顺序 reverse(original)
- 字符串颠倒: return reverse('anagram')//返回"margana"
4.4.5.split函数
字符串拆分(分割),结果为列表 split(original, splitDelimiter)
- 字符串分割: return split("one,two", ",")//返回[“one”,“two”]
 return split(null, splitDelimiter)//返回null.
 return split(original, null)//返回null
4.4.6.toLower和toUpper函数
字符串转换为全小写或者全大写
- 字符串转换为全小写: toLower(original)RETURN toLower("HELLO")//返回"hello"
- 字符串转换为全大写: toLower(original)RETURN toUpper("hello")//返回"HELLO"
4.4.7.toString函数
integer, float, boolean, string, point, duration, date, zoned time, local time, local datetime或zoned datetime值转换为字符串。转换类型不符合则抛异常。
-  转换为字符串: RETURN toString(11.5), toString('already a string'), toString(true), toString(date({year: 1984, month: 10, day: 11})) as dateString, toString(datetime({year: 1984, month: 10, day: 11, hour: 12, minute: 31, second: 14, millisecond: 341, timezone: 'Europe/Stockholm'})) as datetimeString, toString(duration({minutes: 12, seconds: -60})) as durationString返回结果: "11.5" "already a string" "true" "1984-10-11" "1984-10-11T12:31:14.341+01:00[Europe/Stockholm]" "PT11M"
4.4.8.toStringOrNull()函数
integer, float, boolean, string, point, duration, date, zoned time, local time, local datetime或zoned datetime值转换为字符串。转换类型不符合则返回null。
-  转换为字符串: RETURN toStringOrNull(11.5), toStringOrNull('already a string'), toStringOrNull(true), toStringOrNull(date({year: 1984, month: 10, day: 11})) AS dateString, toStringOrNull(datetime({year: 1984, month: 10, day: 11, hour: 12, minute: 31, second: 14, millisecond: 341, timezone: 'Europe/Stockholm'})) AS datetimeString, toStringOrNull(duration({minutes: 12, seconds: -60})) AS durationString, toStringOrNull(['A', 'B', 'C']) AS list返回结果: "11.5" "already a string" "true" "1984-10-11" "1984-10-11T12:31:14.341+01:00[Europe/Stockholm]" "PT11M" <null>
4.4.9.normalize函数
返回标准Unicode normalize(input, normalForm) 默认为NFC,可为NFC, NFD, NFKC和NFKD
- Unicode字符比较: RETURN normalize('\u212B') = '\u00C5' AS result//返回true
4.5.集合(列表)函数
列表是Cypher中的一个重要的复合类型,对列表进行操作的函数主要是生成列表、获取列表对象、抽取特定的列表元素、过滤列表元素和对列表元素进行迭代计算。
4.5.1.keys()函数
返回节点、关系或者map的属性列表
- 返回节点的所有属性: match (n) where n.name = "zhangsan" return keys(n)
- 返回关系的所有属性: match (n)-[r]->(m) where n.name = "zhangsan" and m.name="lisi" return keys(r)
- 返回Map的所有属性(key): with {key1:"value1", key2:"value2"} as map return keys(map)
4.5.2.nodes()函数
从路径(path)中获取所有节点的列表
- nodes(path): 返回list match p=(n)-->(m)-->(s) where n.name="zhangsan" and s.name="wangwu" return nodes(p)
4.5.3.relationships()函数
返回路径(path)上的所有关系
- relationships(path): 返回list match p=(n)-->(m)-->(s) where n.name="zhangsan" and s.name="wangwu" return relationships(p)
4.5.4.extract()函数
从列表中抽取值构成列表 extract(variable in list | expression)1
- 根据抽取的值组装成一个列表,返回一个列表: 返回list match p=(n)-->(m)-->(s) where n.name="zhangsan" and s.name="wangwu" return extract(n in nodes(p)| n.age) as age_list
4.5.5.filter()函数
对列表中的元素进行过滤 filter(variable in list where predicate)
- 过滤列表元素返回新列表: 返回list match (n) where n.name="zhangsan" return n.array, filter(x in a.array where size(x)>=3)
4.5.6.range()函数
用于生成一个integer 类型的列表 range(start, end, step),其中step可以省略,默认值是1。
- 注意: - 返回结果包含start和end
- start、end、step必须是Integer 类型
- 如果start==end,则只返回一个元素的列表
- 如果start > end,则返回一个负数
- 如果start > end,且step < 0,则返回一个递减的列表
 
- 不带step返回列表 return range(0, 10) as list
- 带step返回列表 return range(0, 10, 2) as list
- 返回递减列表 return range(10, 1,-1) as list
4.5.7.reverse()函数
原始列表的元素进行反转
- 反转列表: with [1,2,3,4,5] as list return reverse(list)
4.5.8.tail()函数
跳过列表的第一个元素,在路径遍历的时候会用到
- 跳过列表第一个元素: with [1,2,3,4,5] as list return tail(list)
4.5.9.reduce()函数
返回每个元素作用在表达式上的结果,类似于scala中的reduce函数。
 reduce(accumulator = initial, e in list | expression)
 对列表中的每个元素e进行迭代计算,在元素e上运行表达式(expression),把当前的结果存储在累加器中,进行迭代计算,并返回最终计算的标量结果。
- 列表元素求和: return reduce(total= 0, x in [1,2,3,4] | total+x)
- 计算路径所有节点的age值的和: match p=(n)-->(m)-->(s) where n.name="zhangsan" and m.name="lisi" and s.name"wangwu" return reduce(total=0, k in nodes(p)| total+k.age) as reduction
4.5.10.类型转换函数
-  toBooleanList(): 将List转换为List,如果任何值不能转换为布尔值,那么它们将在返回的LIST中为null。 列表中每个值的转换是根据toBooleanOrNull()函数完成的 
 return toBooleanList(null) as noList, toBooleanList([null, null]) as nullsInList, toBooleanList(['a string', true,'false', null, ['A','B']]) as mixedList
-  toFloatList(): 将List转换为List,如果任何值不能转换为float,那么它们将在返回的List中为null。 列表中每个值的转换是根据toFloatOrNull()函数完成的 
 return toFloatList(null) as noList, toFloatList([null, null]) as nullsInList, toFloatList(['a string', 2.5, '3.14159', null, ['A','B']]) as mixedList
-  toIntegerList(): 将List转换为List,如果任何值不能转换为integer,那么它们将在返回的List中为null。 列表中每个值的转换是根据toIntegerOrNull()函数完成的 
 return toIntegerList(null) as noList, toIntegerList([null, null]) as nullsInList, toIntegerList(['a string', 2, '5', null, ['A','B']]) as mixedList
-  toStringList(): 将List转换为List,如果任何值不能转换为string,那么它们将在返回的List中为null。 列表中每个值的转换是根据toStringOrNull()函数完成的 
 return toStringList(null) as noList, toStringList([null, null]) as nullsInList, toStringList(['already a string', 2, date({year:1955, month:11, day:5}), null, ['A','B']]) as mixedList
- |,管道符号,cypher中列表的推导符号,详细请看cypher语法基础6.5章节 ↩︎