首页 > 其他分享 >neo4j使用详解(六、cypher常用函数语法——最全参考)

neo4j使用详解(六、cypher常用函数语法——最全参考)

时间:2024-03-31 09:33:09浏览次数:23  
标签:返回 cypher return 函数 最全 list neo4j null where

请添加图片描述


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



  1. |,管道符号,cypher中列表的推导符号,详细请看cypher语法基础6.5章节 ↩︎

标签:返回,cypher,return,函数,最全,list,neo4j,null,where
From: https://blog.csdn.net/qq_35754073/article/details/137159156

相关文章

  • lodash已死?radash最全使用介绍(附源码详细说明)—— Array方法篇(1)
    相信很多前端同学甚至非前端都或多或少使用过lodash库,我们都知道lodash是一个非常丰富的前端工具库,比如最常用的防抖和节流,使用lodash都能很快实现,在github上更是有着58.7k的star数。但最近出现的Radash库,号称lodashplus版本,比之更新、更小、更全面、源码更易于理解。阅读本文......
  • 【人工智能入门必看的最全Python编程实战(6)】
    ---------------------------------------------------------------------1.AIGC未来发展前景未完持续…1.1人工智能相关科研重要性拥有一篇人工智能科研论文及专利软著竞赛是保研考研留学深造以及找工作的关键门票!!!拥有一篇人工智能科研论文及专利软著竞赛是保研考研......
  • 基于Java+Neo4j的知识图谱+全文检索的知识库管理系统(附文档+代码)
      在数字化高度普及的时代,企事业机关单位在日常工作中会产生大量的文档,例如医院制度汇编,企业知识共享库等。针对这些文档性的东西,手工纸质化去管理是非常消耗工作量的,并且纸质化查阅难,易损耗,所以电子化管理显得尤为重要。【springboot+elasticsearch+neo4j+vue+activiti】实......
  • 史上最全Docker安装、使用教程!
    安装包下载地址官方rpm包下载地址:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/二进制下载地址:https://mirrors.aliyun.com/docker-ce/linux/static/stable/x86_64/阿里镜像下载地址:https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/s......
  • 从头开始,建立Neo4j图数据库,详细版
    1、前提支持:Java的JDK,在使用Neo4j图数据库之前,电脑上首先要有JDK环境;如果没有下载过JDK请参考:(1)https://www.onlinedown.net/soft/10053430.htm      (2)现在完成的资源如下所示:      (3)通过解压-得到如下所示文件夹:这个文件夹名称很关键;     (4)打......
  • 2024最新最全Java和Go面经,面试了30多场,终于上岸了!
    ​>本文来自我们技术交流群群友的投稿,未经授权,禁止转载。原文链接:太难了,Java和Go,面试了30多场,终于上岸了!先听一下TA的故事2023年10月份我就做好了离职跳槽的准备,做了3年Java后端开发的我,对自己的技术能力还是很有底气的。之前虽不是一线大厂,也算是比较知名的中厂了。加上前公......
  • 银行核心系统应用架构设计最全详述
    我国金融行业信息化建设经过40多年发展,从无到有,从小到大,逐步建立起独立自主、开放创新的科技体系。其中,作为交易中枢的核心系统,通过不断迭代创新,逐步向更加开放、稳定的新一代现代化金融核心系统迈进。伴随着中国金融行业整体发展的现代化,金融核心系统从外部引进到消化吸......
  • 精品推荐-史上最全腾讯全套人力资源管理资料合集(附下载)
    史上最全腾讯全套人力资源管理资料合集,共六个专题。知识星球下载地址:https://t.zsxq.com/18YFTROfG一、腾讯各职位能力模型图1、腾讯-产品经理能力素质模型图(策划运用5个岗位).xls2、腾讯项目经理能力素质模型图.xls3、腾讯研发人员能力素质模型图.xls4、腾讯-技术专业......
  • 全网你能看到最全的RFID整体技术介绍!
    文章目录1RFID简介1.1RFID基本组成1.2工作原理2背景起源2.1工具原型事例2.2理论建立3分类3.1基于工作频率分类3.2基于电子标签是否带电分类4RFID内部组成4.1电子标签4.2读写器4.3软件系统5电子标签详解5.1标签硬件组成5.2标签数据格式内部分区5.3读写锁定操作6RFID......
  • 【整理】2024史上最全的信息安全、数据安全、网络安全标准
    2024史上最全的信息安全、数据安全、网络安全标准下载一、证券JRT0021.4-2023上市公司公告电子化规范第4部分:公司治理类临时公告.pdfJRT0021.3-2023上市公司公告电子化规范第3部分:交易类临时公告.pdfJRT0021.2-2023上市公司公告电子化规范第2部分:首次披露.pdfJRT0021.1......