目录: [TOC]
Cypher运算 Operators
运算概要
运算类型 | 概要 |
---|---|
聚合操作 | DISTINCT |
属性运算 |
. 获取静态属性;[] 获取动态属性;= 替换所有属性 ;+= 修改特定属性 |
数学运算 | +, -, *, /, %, ^ |
比较运算 | =, <>, <, >, <=, >=, IS NULL, IS NOT NULL |
字符串比较运算 | STARTS WITH, ENDS WITH, CONTAINS |
布尔运算 | AND, OR, XOR, NOT |
字符串运算 |
+ 字符串连接;=~ 正则匹配 |
时间运算 |
+, - ,*, / |
Map运算 |
. 静态获取value;[] 动态获取value |
List运算 |
+ 链表连接;IN 判断是否在链表;[] 动态获取元素 |
聚合运算
DISTINCT
删除重复元素
CREATE (a:Person { name: 'Anne', eyeColor: 'blue' }),(b:Person { name: 'Bill', eyeColor: 'brown' }),(c:Person { name: 'Carol', eyeColor: 'blue' })
WITH [a, b, c] AS ps
UNWIND ps AS p
RETURN DISTINCT p.eyeColor
Result:
p.eyeColor
"blue"
"brown"
2 rows, Nodes created: 3 Properties set: 6 Labels added: 3
其中,WITH [a, b, c] AS ps
和WITH [a, b, c] AS ps
作用是把a, b,c
s三个实例当作一个变量处理;
属性运算
属性运算包括节点和关系的运算:
- 静态获取属性使用
.
运算符 - 动态获取属性使用
[ ]
- 属性替换使用
=
- 属性修改
+=
静态获取属性
CREATE (a:Person { name: 'Jane', livesIn: 'London' }),(b:Person { name: 'Tom', livesIn: 'Copenhagen' })
WITH a, b
MATCH (p:Person)
RETURN p.name
Result
p.name
"Jane"
"Tom"
2 rows, Nodes created: 2 Properties set: 4 Labels added: 2
动态获取属性
CREATE (a:Restaurant { name: 'Hungry Jo', rating_hygiene: 10, rating_food: 7 }),(b:Restaurant { name: 'Buttercup Tea Rooms', rating_hygiene: 5, rating_food: 6 }),(c1:Category { name: 'hygiene' }),(c2:Category { name: 'food' })
WITH a, b, c1, c2
MATCH (restaurant:Restaurant),(category:Category)
WHERE restaurant["rating_" + category.name]> 6
RETURN DISTINCT restaurant.name
Result
restaurant.name
"Hungry Jo"
1 row, Nodes created: 4 Properties set: 8 Labels added: 4
属性替换
- 所有存在的属性都会被替换
CREATE (a:Person { name: 'Jane', age: 20 })
WITH a
MATCH (p:Person { name: 'Jane' })
SET p = { name: 'Ellen', livesIn: 'London' }
RETURN p.name, p.age, p.livesIn
修改属性
CREATE (a:Person { name: 'Jane', age: 20 })
WITH a
MATCH (p:Person { name: 'Jane' })
SET p += { name: 'Ellen', livesIn: 'London' }
RETURN p.name, p.age, p.livesIn
数学运算
数学运算包括
- addition: +
- subtraction or unary minus: -
- multiplication: *
- division: /
- modulo division: %
- exponentiation: ^
比较运算
比较运算包括:
- equality: =
- inequality: <>
- less than: <
- greater than: >
- less than or equal to: <=
- greater than or equal to: >=
- IS NULL
- IS NOT NULL
字符串比较运算
-
STARTS WITH
匹配前缀 -
ENDS WITH
匹配后缀 -
CONTAINS
字符串包含
WITH ['John', 'Mark', 'Jonathan', 'Bill'] AS somenames
UNWIND somenames AS names
WITH names AS candidate
WHERE candidate STARTS WITH 'Jo'
RETURN candidate
布尔运算
- conjunction: AND
- disjunction: OR,
- exclusive disjunction: XOR
- negation: NOT
WITH [2, 4, 7, 9, 12] AS numberlist
UNWIND numberlist AS number
WITH number
WHERE number = 4 OR (number > 6 AND number < 10)
RETURN number
字符串运算
- 字符串连接:
+
- 正则匹配:
=~
WITH ['mouse', 'chair', 'door', 'house'] AS wordlist
UNWIND wordlist AS word
WITH word
WHERE word =~ '.*ous.*'
RETURN word
时间运算
时间运算包括:
-
+ ,-
:Duration与时间实例或者Duration -
* , /
:Duration与一个数字
WITH localdatetime({ year:1984, month:10, day:11, hour:12, minute:31, second:14 }) AS aDateTime,
duration({ years: 12, nanoseconds: 2 }) AS aDuration
RETURN aDateTime + aDuration, aDateTime - aDuration
- duration应用到时间实例上没有的部分将被忽略,如下所示
WITH date({ year:1984, month:10, day:11 }) AS aDate, duration({ years: 12, nanoseconds: 2 }) AS aDuration
RETURN aDate + aDuration, aDate - aDuration
- Adding two durations to a temporal instant is not an associative operation. This is because non-existing dates are truncated to the nearest existing date:
RETURN
(date("2011-01-31")+ duration("P1M"))+ duration("P12M") AS date1,
date("2011-01-31")+(duration("P1M")+ duration("P12M")) AS date2
- Duration 加减 Duration
WITH duration({ years: 12, months: 5, days: 14, hours: 16, minutes: 12, seconds: 70, nanoseconds: 1 }) AS duration1,
duration({ months:1, days: -14, hours: 16, minutes: -12, seconds: 70 }) AS duration2
RETURN duration1, duration2, duration1 + duration2, duration1 - duration2
- Duration 乘除number
WITH duration({ days: 14, minutes: 12, seconds: 70, nanoseconds: 1 }) AS aDuration
RETURN aDuration, aDuration * 2, aDuration / 3
Map运算
-
静态获取Map的value,使用点运算符
-
动态获取Map的value,使用[]
-
静态
WITH { person: { name: 'Anne', age: 25 }} AS p
RETURN p.person.name
- 动态
参数:
{
"myKey" : "name"
}
WITH { name: 'Anne', age: 25 } AS a
RETURN a[$myKey] AS result
result1
"Anne"
1row
List运算
-
List链接:+
-
List 判断e是否在List中:e IN List
-
动态获取其中元素:[]
-
List连接
RETURN [1,2,3,4,5]+[6,7] AS myList
- 判断元素是否List中
WITH [2, 3, 4, 5] AS numberlist
UNWIND numberlist AS number
WITH number
WHERE number IN [2, 3, 8]
RETURN number
- 动态获取其中元素
WITH ['Anne', 'John', 'Bill', 'Diane', 'Eve'] AS names
RETURN names[1..3] AS result
Cypher注释
-
//
表示注释
MATCH (n)
//This is a whole line comment
RETURN n
Cypher模式
模式和模式匹配是Cypher的核心。
MATCH, CREATE and MERGE
语句用于关联
节点模式
- 最简单的节点模式,下面例子描述了一个节点,并使用变量a
(a)
相关节点模式
- 描述多个节点和节点之间的关系,
# 下面例子描述了两个节点一个关系,分别用变量a,b代表节点;
(a)-->(b)
# 描述多个节点和节点关系
# 节点和关系序列被称为path 路径
(a)-->(b)<--(c)
# 不需要命名节点是,可以不写变量
(a)-->()<--(c)
标签模式
# 带有标签的节点
(a:User)-->(b)
# 多个标签的节点
(a:User:Admin)-->(b)
指定属性
# 节点属性,使用Map结构表示属性
(a {name: 'Andy', sport: 'Brazilian Ju-Jitsu'})
# 关系属性
(a)-[{blocked: false}]->(b)
关系模式
# 可以按照之前描述有方向的关系,如果不需要关系方向,可以忽略箭头
(a)--(b)
# 给关系取个名
(a)-[r]->(b)
# 匹配带标签的关系
(a)-[r:REL_TYPE]->(b)
# 关系只能有一个标签,如果需要描述标签集合可以使用如下描述
(a)-[r:TYPE1|TYPE2]->(b)
# 不需要给关系赋值给变量,可以这么写
(a)-[:REL_TYPE]->(b)
变长模式匹配
# 描述指定长度的关系,描述了三个节点两个关系
(a)-[*2]->(b) # 等价于 (a)-->()-->(b)
# 长度范围也可以指定,下面指定最短为3,最长为5
(a)-[*3..5]->(b)
# 至少三个关系
(a)-[*3..]->(b)
# 最多5个关系
(a)-[*..5]->(b)
# 任意长度的关系
(a)-[*]->(b)
例子:
MATCH (me)-[:KNOWS*1..2]-(remote_friend)
WHERE me.name = 'Filipa'
RETURN remote_friend.name
remote_friend.name
"Dilshad"
"Anders"
2 rows
路径赋值
// 一系列的节点和关系,称为路径
// 可以在MATCH, CREATE and MERGE中使用,不能在模式表达式使用
p = (a)-[*3..5]->(b)
时间值
Lists
Maps
坐标值Spatial values
本文使用 文章同步助手 同步
网友评论