neo4j查询语言Cypher详解(三)--函数

函数

Cypher中的函数如果输入参数为null,则返回null。

以字符串作为输入的函数都对Unicode字符进行操作,而不是对标准字符进行操作。例如,size()函数应用于任何Unicode字符将返回1,即使该字符不适合一个字符的16位。

可以通过 SHOW FUNCTIONS 查看函数定义。

函数签名中参数格式:eg:

cypher 复制代码
all(
    variable :: VARIABLE 		//:: VARIABLE,说明是个变量,可用于后面的 WHERE 部分
	IN 
list :: LIST OF ANY? 			//list,是个LIST泛型。
WHERE predicate :: ANY?			// predicate,是任意的断言
) 
:: (BOOLEAN?)					//函数返回值类型为BOOLEAN

断言函数

Function Signature Description
all() all(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) Returns true if the predicate holds for all elements in the given list.
any() any(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) Returns true if the predicate holds for at least one element in the given list.
exists() exists(input :: ANY?) :: (BOOLEAN?) Returns true if a match for the pattern exists in the graph.
isEmpty() isEmpty(input :: LIST? OF ANY?) :: (BOOLEAN?) Checks whether a list is empty.
isEmpty() isEmpty(input :: MAP?) :: (BOOLEAN?) Checks whether a map is empty.
isEmpty() isEmpty(input :: STRING?) :: (BOOLEAN?) Checks whether a string is empty.
none() none(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) Returns true if the predicate holds for no element in the given list.
single() single(variable :: VARIABLE IN list :: LIST OF ANY? WHERE predicate :: ANY?) :: (BOOLEAN?) Returns true if the predicate holds for exactly one of the elements in the given list.

标量函数

Function Signature Description
coalesce() coalesce(input :: ANY?) :: (ANY?) 返回第一个非null值。
endNode() endNode(input :: RELATIONSHIP?) :: (NODE?) Returns the end node of a relationship.
head() head(list :: LIST? OF ANY?) :: (ANY?) Returns the first element in a list.
id() id(input :: NODE?) :: (INTEGER?) Deprecated Returns the id of a node. Replaced by elementId()
id(input :: RELATIONSHIP?) :: (INTEGER?) Deprecated Returns the id of a relationship. Replaced by elementId().
last() last(list :: LIST? OF ANY?) :: (ANY?)
length() length(input :: PATH?) :: (INTEGER?) Returns the length of a path.
properties() properties(input :: MAP?) :: (MAP?) 返回一个对象的所有属性,作为一个map
properties(input :: NODE?) :: (MAP?) 返回一个节点的所有属性,作为一个map
properties(input :: RELATIONSHIP?) :: (MAP?) 返回一个关系的所有属性,作为一个map
randomUUID() randomUUID() :: (STRING?)
size() size(input :: LIST? OF ANY?) :: (INTEGER?) Returns the number of items in a list.
size(input :: STRING?) :: (INTEGER?) Returns the number of Unicode characters in a string.
startNode() startNode(input :: RELATIONSHIP?) :: (NODE?)
toBoolean() toBoolean(input :: STRING?) :: (BOOLEAN?)
toBoolean(input :: BOOLEAN?) :: (BOOLEAN?)
toBoolean(input :: INTEGER?) :: (BOOLEAN?)
toBooleanOrNull() toBooleanOrNull(input :: ANY?) :: (BOOLEAN?) 转换为boolean,不能转换返回null。
toFloat() toFloat(input :: NUMBER?) :: (FLOAT?)
toFloat(input :: STRING?) :: (FLOAT?)
toFloatOrNull() toFloatOrNull(input :: ANY?) :: (FLOAT?) 转换为小数,不能转换返回null。
toInteger() toInteger(input :: NUMBER?) :: (INTEGER?)
toInteger(input :: BOOLEAN?) :: (INTEGER?)
toInteger(input :: STRING?) :: (INTEGER?)
toIntegerOrNull() toIntegerOrNull(input :: ANY?) :: (INTEGER?) 转换为整形,不能转换返回null。
type() type(input :: RELATIONSHIP?) :: (STRING?) Returns the string representation of the relationship type.

示例

cypher 复制代码
CREATE (p:Person {name: 'Stefan', city: 'Berlin'})
RETURN properties(p)
//OUT PUT:
//: {"city":"Berlin","name":"Stefan"}

聚合函数

Function Signature Description
avg() avg(input :: DURATION?) :: (DURATION?)
avg(input :: FLOAT?) :: (FLOAT?)
avg(input :: INTEGER?) :: (INTEGER?)
collect() collect(input :: ANY?) :: (LIST? OF ANY?) 收集数据作为一个LIST。
count() count(input :: ANY?) :: (INTEGER?)
max() max(input :: ANY?) :: (ANY?)
min() min(input :: ANY?) :: (ANY?)
percentileCont() percentileCont(input :: FLOAT?, percentile :: FLOAT?) :: (FLOAT?)
percentileDisc() percentileDisc(input :: FLOAT?, percentile :: FLOAT?) :: (FLOAT?)
`percentileDisc(input :: INTEGER?, percentile :: FLOAT?) :: (INTEGER?)
stdev() stdev(input :: FLOAT?) :: (FLOAT?)
stdevp() stdevp(input :: FLOAT?) :: (FLOAT?)
sum() sum(input :: DURATION?) :: (DURATION?)
`sum(input :: FLOAT?) :: (FLOAT?)
`sum(input :: INTEGER?) :: (INTEGER?)

集合函数

Function Signature Description
keys() keys(input :: MAP?) :: (LIST? OF STRING?) 返回MAP的所有KEY LIST
keys(input :: NODE?) :: (LIST? OF STRING?) 返回节点的所有属性 LIST
keys(input :: RELATIONSHIP?) :: (LIST? OF STRING?) 返回关系的所有属性 LIST
labels() labels(input :: NODE?) :: (LIST? OF STRING?) Returns a list containing the string representations for all the labels of a node.
nodes() nodes(input :: PATH?) :: (LIST? OF NODE?) Returns a list containing all the nodes in a path.
range() range(start :: INTEGER?, end :: INTEGER?) :: (LIST? OF INTEGER?) Returns a list comprising all integer values within a specified range.
range(start :: INTEGER?, end :: INTEGER?, step :: INTEGER?) :: (LIST? OF INTEGER?) Returns a list comprising all integer values within a specified range created with step length.
reduce() `reduce(accumulator :: VARIABLE = initial :: ANY?, variable :: VARIABLE IN list :: LIST OF ANY? expression :: ANY) :: (ANY?)`
relationships() relationships(input :: PATH?) :: (LIST? OF RELATIONSHIP?) Returns a list containing all the relationships in a path.
reverse() reverse(input :: LIST? OF ANY?) :: (LIST? OF ANY?) Returns a list in which the order of all elements in the original list have been reversed.
tail() tail(input :: LIST? OF ANY?) :: (LIST? OF ANY?) Returns all but the first element in a list.
toBooleanList() toBooleanList(input :: LIST? OF ANY?) :: (LIST? OF BOOLEAN?)
toFloatList() toFloatList(input :: LIST? OF ANY?) :: (LIST? OF FLOAT?)
toIntegerList() toIntegerList(input :: LIST? OF ANY?) :: (LIST? OF INTEGER?)
toStringList() toStringList(input :: LIST? OF ANY?) :: (LIST? OF STRING?)

示例

toBooleanList()

cypher 复制代码
RETURN toBooleanList(null) as noList,
toBooleanList([null, null]) as nullsInList,
toBooleanList(['a string', true, 'false', null, ['A','B']]) as mixedList
noList nullsInList mixedList
<null> [<null>,<null>] [<null>,true,false,<null>,<null>]

总结:

  • 参数不是个LIST,报错
  • LIST 中的null,不转换,保留
  • LIST中的不可转换元素,结果为null。
  • BOOLEAN类型的元素,保留原始值

toFloatListtoIntegerListtoStringListtoBooleanList 规则类似。

数值函数

Function Signature Description
abs() abs(input :: FLOAT?) :: (FLOAT?)
abs(input :: INTEGER?) :: (INTEGER?)
ceil() ceil(input :: FLOAT?) :: (FLOAT?)
floor() floor(input :: FLOAT?) :: (FLOAT?)
isNaN() isNaN(input :: FLOAT?) :: (BOOLEAN?) Returns true if the floating point number is NaN.
isNaN(input :: INTEGER?) :: (BOOLEAN?)
rand() rand() :: (FLOAT?)
round() round(input :: FLOAT?) :: (FLOAT?)
round(value :: FLOAT?, precision :: NUMBER?) :: (FLOAT?)
round(value :: FLOAT?, precision :: NUMBER?, mode :: STRING?) :: (FLOAT?)
sign() sign(input :: FLOAT?) :: (INTEGER?)
sign(input :: INTEGER?) :: (INTEGER?)

对数函数

Function Signature Description
e() e() :: (FLOAT?) 返回e。
exp() exp(input :: FLOAT?) :: (FLOAT?) 返回e^n。
log() log(input :: FLOAT?) :: (FLOAT?) 返回自然对数,以e为底
log10() log10(input :: FLOAT?) :: (FLOAT?) 返回以10位底的对数
sqrt() sqrt(input :: FLOAT?) :: (FLOAT?) 平方差

三角函数

Function Signature Description
acos() acos(input :: FLOAT?) :: (FLOAT?)
asin() `asin(input :: FLOAT?) :: (FLOAT?)
atan() atan(input :: FLOAT?) :: (FLOAT?)
atan2() atan2(y :: FLOAT?, x :: FLOAT?) :: (FLOAT?)
cos() cos(input :: FLOAT?) :: (FLOAT?)
cot() cot(input :: FLOAT?) :: (FLOAT?)
degrees() degrees(input :: FLOAT?) :: (FLOAT?) 将弧度转换为角度
haversin() haversin(input :: FLOAT?) :: (FLOAT?) 半正矢计算
pi() pi() :: (FLOAT?)
radians() radians(input :: FLOAT?) :: (FLOAT?) 将角度转换为弧度
sin() sin(input :: FLOAT?) :: (FLOAT?)
tan() tan(input :: FLOAT?) :: (FLOAT?)

字符串函数

Function Signature Description
left() left(original :: STRING?, length :: INTEGER?) :: (STRING?)
ltrim() ltrim(input :: STRING?) :: (STRING?)
replace() replace(original :: STRING?, search :: STRING?, replace :: STRING?) :: (STRING?)
reverse() reverse(input :: STRING?) :: (STRING?)
right() `right(original :: STRING?, length :: INTEGER?) :: (STRING?)
rtrim() rtrim(input :: STRING?) :: (STRING?)
split() split(original :: STRING?, splitDelimiter :: STRING?) :: (LIST? OF STRING?)
split(original :: STRING?, splitDelimiters :: LIST? OF STRING?) :: (LIST? OF STRING?)
substring() substring(original :: STRING?, start :: INTEGER?) :: (STRING?) 从0开始
substring(original :: STRING?, start :: INTEGER?, length :: INTEGER?) :: (STRING?)
toLower() toLower(input :: STRING?) :: (STRING?)
toString() toString(input :: ANY?) :: (STRING?)
toStringOrNull() toStringOrNull(input :: ANY?) :: (STRING?)
toUpper() toUpper(input :: STRING?) :: (STRING?)
trim() trim(input :: STRING?) :: (STRING?)

时间函数

Function Signature Description
date() date(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) 创建DATE
date.realtime() date.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) 使用 realtime clock 创建当前DATE
date.statement() date.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) 使用 statement clock 创建当前DATE
date.transaction() date.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATE?) 使用 transaction clock 创建当前DATE
date.truncate() date.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (DATE?) 截断DATE
datetime() datetime(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?)
datetime.fromepoch() datetime.fromepoch(seconds :: NUMBER?, nanoseconds :: NUMBER?) :: (DATETIME?)
datetime.fromepochmillis() datetime.fromepochmillis(milliseconds :: NUMBER?) :: (DATETIME?)
datetime.realtime() datetime.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?)
datetime.statement() datetime.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?)
datetime.transaction() datetime.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (DATETIME?)
datetime.truncate() datetime.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (DATETIME?)
localdatetime() localdatetime(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?)
localdatetime.realtime() localdatetime.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?)
localdatetime.statement() localdatetime.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?)
localdatetime.transaction() localdatetime.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALDATETIME?)
localdatetime.truncate() localdatetime.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (LOCALDATETIME?)
localtime() localtime(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?)
localtime.realtime() localtime.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?)
localtime.statement() localtime.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?)
localtime.transaction() localtime.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (LOCALTIME?)
localtime.truncate() localtime.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (LOCALTIME?)
time() time(input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?)
time.realtime() time.realtime(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?)
time.statement() time.statement(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?)
time.transaction() time.transaction(timezone = DEFAULT_TEMPORAL_ARGUMENT :: ANY?) :: (TIME?)
time.truncate() time.truncate(unit :: STRING?, input = DEFAULT_TEMPORAL_ARGUMENT :: ANY?, fields = null :: MAP?) :: (TIME?)

时钟控制

  • transaction: 同一个事务,时间返回相同值。
  • statement: 同一个语句的同一次调用,时间返回相同值。
  • realtime: 系统时间。

截断单位

  • millennium:千年
  • century:世纪
  • decade:十年
  • year:
  • weekYear:
  • quarter:
  • month:
  • week:
  • day:
  • hour:
  • minute:
  • second:
  • millisecond: 毫秒
  • microsecond: 微秒

示例

cypher 复制代码
date([{timezone}])
RETURN date() AS currentDate
RETURN date({timezone: 'America/Los Angeles'}) AS currentDateInLA

date({year [, month, day]})
UNWIND [
date({year: 1984, month: 10, day: 11}),
date({year: 1984, month: 10}),
date({year: 1984})
] AS theDate
RETURN theDate

date({year [, week, dayOfWeek]})
UNWIND [
date({year: 1984, week: 10, dayOfWeek: 3}),
date({year: 1984, week: 10}),
date({year: 1984})
] AS theDate
RETURN theDate

所有时间函数,都支持通过一个Map对象构造时间实例。

duration函数

Function Signature Description
duration() duration(input :: ANY?) :: (DURATION?)
duration.between() duration.between(from :: ANY?, to :: ANY?) :: (DURATION?) 计算2个时间差
duration.inDays() duration.inDays(from :: ANY?, to :: ANY?) :: (DURATION?)
duration.inMonths() duration.inMonths(from :: ANY?, to :: ANY?) :: (DURATION?)
duration.inSeconds() duration.inSeconds(from :: ANY?, to :: ANY?) :: (DURATION?)

示例

cypher 复制代码
duration([ {years, quarters, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds} ])
            
duration(temporalAmount)   // temporalAmount STRING类型
            

duration标准字符串格式:P[nY][nM][nW][nD][T[nH][nM][nS]],具体见:https://neo4j.com/docs/cypher-manual/current/values-and-types/temporal/#cypher-temporal-specifying-durations

cypher 复制代码
duration.between(instant1, instant2)
                 

字段

  • years
  • quarters
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds
  • milliseconds
  • microseconds
  • nanoseconds

空间函数

Function Signature Description
point.distance() `point.distance(from :: POINT?, to :: POINT?) :: (FLOAT?)
point() - Cartesian 2D point(input :: MAP?) :: (POINT?)
point() - Cartesian 3D point(input :: MAP?) :: (POINT?)
point() - WGS 84 2D `point(input :: MAP?) :: (POINT?)
point() - WGS 84 3D `point(input :: MAP?) :: (POINT?)
point.withinBBox() point.withinBBox(point :: POINT?, lowerLeft :: POINT?, upperRight :: POINT?) :: (BOOLEAN?)

加载 CSV 函数

Function Signature Description
file() file() :: (STRING?) 返回文件路径
linenumber() linenumber() :: (INTEGER?) 返回行数

Graph functions

Function Signature Description
graph.names() graph.names() :: (LIST? OF STRING?)
graph.propertiesByName() graph.propertiesByName(name :: STRING?) :: (MAP?)
graph.byName() USE graph.byName(name :: STRING?)

用户自定义函数

Type Description Usage Developing
Scalar For each row the function takes parameters and returns a result. Using UDF Extending Neo4j (UDF)
Aggregating Consumes many rows and produces an aggregated result. Using aggregating UDF Extending Neo4j (Aggregating UDF)

附录

参考

https://neo4j.com/docs/cypher-manual/current/functions/

相关推荐
日里安1 小时前
8. 基于 Redis 实现限流
数据库·redis·缓存
EasyCVR1 小时前
ISUP协议视频平台EasyCVR视频设备轨迹回放平台智慧农业视频远程监控管理方案
服务器·网络·数据库·音视频
Elastic 中国社区官方博客1 小时前
使用真实 Elasticsearch 进行更快的集成测试
大数据·运维·服务器·数据库·elasticsearch·搜索引擎·集成测试
明月与玄武2 小时前
关于性能测试:数据库的 SQL 性能优化实战
数据库·sql·性能优化
PGCCC3 小时前
【PGCCC】Postgresql 存储设计
数据库·postgresql
PcVue China5 小时前
PcVue + SQL Grid : 释放数据的无限潜力
大数据·服务器·数据库·sql·科技·安全·oracle
魔道不误砍柴功7 小时前
简单叙述 Spring Boot 启动过程
java·数据库·spring boot
锐策7 小时前
〔 MySQL 〕数据库基础
数据库·mysql
远歌已逝8 小时前
管理Oracle实例(二)
数据库·oracle
日月星宿~8 小时前
【MySQL】summary
数据库·mysql