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

请添加图片描述


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章节 ↩︎

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/783563.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据结构--循环链表(C语言实现)

一.循环链表的设计 typedef struct CNode{ int data; struct CNode* next; }CNode ,*CList; 2.循环链表的示意图: 3.循环链表和单链表的区别: 唯一区别,没有空指针,尾节点的后继为头,为循环之意. 二.循环链表的实现 //初始化return true; }//返回key的前驱地址&#xff0c;如果…

Lazarus远控组件NukeSped分析

静态信息&#xff1a; 样本md5&#xff1a;9b656f5d7e679b94e7b91fc3c4f313e4 由此可见为假的Adobe Flash Player 的攻击样本 样本分析 通过五个函数&#xff0c;内部调用sub_40159D函数动态获取API函数 利用IDA python解密字符串。。 完整python代码 Python> idc.get_…

MongoDB副本集环境搭建(以单机Windows为例)

前言 近期有搭建MongoDB副本集的需求,简单记录一下搭建过程(以本地Windows环境为例)。 一、副本集选型 1 Primary节点、1 Secondary 节点、1 Arbiter节点模式副本集环境搭建。 二、搭建过程 1. 安装MongoDB服务 下载地址:https://www.mongodb.com,如下图所示: 选择…

基于Springboot旅游网站管理系统设计和实现

基于Springboot旅游网站管理系统设计和实现 博主介绍&#xff1a;多年java开发经验&#xff0c;专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 ⭐留言 文末获取源码联系…

7.卷积神经网络与计算机视觉

计算机视觉是一门研究如何使计算机识别图片的学科&#xff0c;也是深度学习的主要应用领域之一。 在众多深度模型中&#xff0c;卷积神经网络“独领风骚”&#xff0c;已经被称为计算机视觉的主要研究根据之一。 一、卷积神经网络的基本思想 卷积神经网络最初由 Yann LeCun&a…

UE4_碰撞_自定义碰撞检测通道

效果如图&#xff1a; 1、项目设置中新建追踪检测通道weapon&#xff0c;默认值为忽略。 2、新建几个actor作为枪&#xff0c;碰撞预设全部设为自定义&#xff0c;把新建的检测响应weapon设为阻挡。 3、角色进行射线检测 运行效果如下&#xff1a; 发现有些物体碰不到&#xff…

GetSystemTimes:获取CPU占用率(WIN API)

原文链接&#xff1a;https://blog.csdn.net/qq_28742901/article/details/104960653 GetSystemTimes函数&#xff1a; BOOL WINAPI GetSystemTimes(__out_opt LPFILETIME lpIdleTime, // 空闲时间__out_opt LPFILETIME lpKernelTime, // 内核进程占用时间__out_opt LPFILETI…

蓝桥杯 本质上升序列

题目描述: 小蓝特别喜欢单调递增的事物。 在一个字符串中&#xff0c;如果取出若干个字符&#xff0c;将这些字符按照在字符串中的顺序排列后是单调递增的&#xff0c;则成为这个字符串中的一个单调递增子序列。 例如&#xff0c;在字符串 lanqiao 中&#xff0c;如果取出字符…

模型训练----将pth模型转换为onnx

目录 1 安装需要的环境2、模型转换3、测试onnx模型 Github代码 1 安装需要的环境 需要在虚拟环境中安装onnx和onnxruntime&#xff08;GPU&#xff09;&#xff0c;环境和自己的cuda版本要对应上查询链接 激活环境&#xff0c;查看环境的cuda版本,我是cuda11.6 cudnn8302&a…

AI预测福彩3D第22弹【2024年3月31日预测--第5套算法开始计算第4次测试】

今天&#xff0c;咱们继续进行本套算法的测试&#xff0c;今天为第四次测试&#xff0c;仍旧是采用冷温热趋势结合AI模型进行预测。好了&#xff0c;废话不多说了。直接上结果~ 仍旧是分为两个方案&#xff0c;1大1小。 经过人工神经网络计算并进行权重赋值打分后&#xff0c;3…

开源翻译大模型

开源翻译大模型 1 简介 在开发过程中&#xff0c;会遇到定制化翻译工具的需要&#xff0c;开源的翻译模型可以解决相应的问题。其中英语转中文的比较好的开源项目有&#xff1a; 序号组织模型地址备注1赫尔辛基大学语言技术研究小组&#xff08;Language Technology Researc…

如何使用Axure RP制作网页原型并结合IIS服务实现公网访问本地HTML网页

文章目录 前言1.在AxureRP中生成HTML文件2.配置IIS服务3.添加防火墙安全策略4.使用cpolar内网穿透实现公网访问4.1 登录cpolar web ui管理界面4.2 启动website隧道4.3 获取公网URL地址4.4. 公网远程访问内网web站点4.5 配置固定二级子域名公网访问内网web站点4.5.1创建一条固定…

SSH免密登录——linux

SSH免密登录——linux 方法一一、用 ssh-key-gen 在本地主机上创建公钥和密钥二、用 ssh-copy-id 把客户端公钥追加到远程主机的 .ssh/authorized_key 上三、直接登录远程主机 方法二一、将生成的客户端公钥id_rsa.pub内容追加至目标主机.ssh/authorized_key 中参考链接 SSH免密…

动态规划-----背包类问题(0-1背包与完全背包)详解

目录 什么是背包问题&#xff1f; 动态规划问题的一般解决办法&#xff1a; 0-1背包问题&#xff1a; 0 - 1背包类问题 分割等和子集&#xff1a; 完全背包问题&#xff1a; 完全背包类问题 零钱兑换II: 什么是背包问题&#xff1f; 背包问题(Knapsack problem)是一种…

日历插件fullcalendar【笔记】

日历插件fullcalendar【笔记】 前言版权开源推荐日历插件fullcalendar一、下载二、初次使用日历界面示例-添加事件&#xff0c;删除事件 三、汉化四、动态数据五、前后端交互1.环境搭建-前端搭建2.环境搭建-后端搭建3.代码编写-前端代码fullcalendar.htmlfullcalendar.js 4.代码…

【更新】在湘源7、8中使用2023年11月国空用地用海分类

之前为了做控规&#xff0c;从湘源8中扒了一套国空用地用海的绘图参数给湘源7使用。 【预告】在湘源控规7中使用 国空用地用海分类标准 但是部里在2023年11月又发布了一套新的用地用海分类。 本想去湘源8里面再扒一下&#xff0c;结果发现湘源8自己还没有更新呢&#xff0c;…

redis学习-redis配置文件解读

目录 1.单位说明 2. include配置 3. network网络配置 3.1 bind绑定ip配置 3.2保护模式protected-mode配置 3.3端口号port配置​编辑 3.4超时断开连接timeout配置 4. general通用配置 4.1守护进程模式daemonize配置 4.2进程id存放文件pidfile配置 4.3日志级别loglevel配置 4.…

实时数仓建设实践——滴滴实时数据链路组件的选型

目录 前言 一、实时数据开发在公司内的主要业务场景 二、实时数据开发在公司内的通用方案 三、特定场景下的实时数据开发组件选型 3.1 实时指标监控场景 3.2 实时BI分析场景 3.3 实时数据在线服务场景 3.4 实时特征和标签系统 四、各组件资源使用原则 五、总结和展望…

浅读 Natural Language Generation Model for Mammography Reports Simulation

浅读 Natural Language Generation Model for Mammography Reports Simulation 这是一篇报告生成 去伪 的文章&#xff0c;重点看生成报告的 真实性 Abstract Extending the size of labeled corpora of medical reports is a major step towards a successful training of …

手搓 Docker Image Creator(DIC)工具(02):预备知识

此节主要简单介绍一下 Docker、Dockerfile 的基本概念&#xff0c;Dockerfile 对的基本语法&#xff0c;Windows 和 macOS 下 Docker 桌面的安装&#xff0c;Docker 镜像的创建和运行测试等。 1 关于 Docker Docker 是一个开源的应用容器引擎&#xff0c;它允许开发者打包应用…