当前位置 : 主页 > 操作系统 > centos >

Cypher学习《二》

来源:互联网 收集:自由互联 发布时间:2023-09-06
1.optional match optional match语句用于搜索模式中描述的匹配项,对于找不到的项用null代替。 optional match匹配模式与match类似,不同之处在于,如果没有匹配到,optional match将null作为未匹配到

1.optional match

optional match语句用于搜索模式中描述的匹配项,对于找不到的项用null代替。

optional match匹配模式与match类似,不同之处在于,如果没有匹配到,optional match将null作为未匹配到部分的值,optional match在cypher中类似于sql中的out join

create (a:Person {name:'Charlie Sheen'})
create (b:Person {name:'Martin Sheen'})
create (c:Person {name:'Michael Douglas'})
create (d:Person {name:'Oliver Stone'})
create (e:Person {name:'Rob Reiner'})
create (f:Movie {title:'Wall Street',name:'Wall Street'})
create (g:Movie {title:'The American President',name:'The American President'})
create (d)-[:directed]->(f),
(e)-[:directed]->(g),
(c)-[:acted_in {roles:['Gordon Gekko']}]->(f),
(a)-[:acted_in {roles:['Bud Fox']}]->(f),
(a)-[:father {roles:['xxx']}]->(b),
(b)-[:acted_in {roles:['Carl Fox']}]->(f),
(c)-[:acted_in {roles:['President Andrew Shepherd']}]->(g),
(b)-[:acted_in {roles:['A.J.MacInerney']}]->(g)

Cypher学习《二》_字符串

查询关系,如果存在就返回,否则就在相应的地方返回null
match (f:Movie {title:'Wall Street'})
optional match (f)-->(x)
return x

Cypher学习《二》_正则表达式_02

可选元素属性

如果可选的元素为null,那么该元素的属性也返回null

match (f:Movie {title:'Wall Street'})
optional match (f)-->(x)
return x,x.name

Cypher学习《二》_运算符_03

可选的关系类型

可以在查询中指定可选的关系类型,下面例子返回null关系,因为该节点没有acts_in的外向关系

match (f:Movie {title:'Wall Street'})
optional match (f)-[r:acted_in]->()
return r

Cypher学习《二》_运算符_04

2.where

where在match或者optional match语句中添加约束,或者与with一起使用来过滤结果,where不能单独使用,它只能作为match、optional match、start和with的一部分, 如果是在with和start中,它用于过滤结果。对于match和optional match,where为模式增加约束,它不能看做是匹配完成后的结果过滤

create (a:Swedish {name:'Andres',age:36,belt:'white'})
create (b:Swedish {email:'peter_n@example.com',name:'Peter',age:34})
create (c:Swedish {address:'Sweden/MaImo',name:'Tobias',age:25})
create (a)-[:knows {since:1999}]->(b),
(a)-[:knows {since:2012}]->(c)

Cypher学习《二》_字符串_05

基本使用

布尔运算

可以在where中使用布尔运算符,如and、or、not

match (n)
where n.name = 'Peter' or (n.age<30 and n.name = 'Tobias') or not (n.name = 'Tobias' or n.name = 'Peter')
return n

Cypher学习《二》_字符串_06

节点标签过滤

可以在where中类似使用where n:foo 写入标签断言来过滤节点

match (n)
where n:Swedish
return n
节点属性过滤

可以在where语句中对节点的属性进行过滤

match (n)
where n.age < 30
return n

Cypher学习《二》_字符串_07

关系属性的过滤
match (n)-[k:knows]->(f)
where k.since < 2000
return f

Cypher学习《二》_正则表达式_08

动态属性节点过滤(目前报错)

以方括号语法的形式可使用动态计算的值来过滤属性

{"prop":"AGE"},
match (n)
where n [toLower($prop)]<30
return n
属性存在性检查

使用exists()只能检查节点或者关系的某个属性是否存在

match (n)
where exists (n.belt)
return n

Cypher学习《二》_字符串_09

3.字符串匹配

可以使用starts with和ends with来匹配字符串的开始和结尾,如果不关心匹配字符串的位置,可以使用contains,匹配是区分大小写。

匹配字符串的开始

start with用于大小写敏感的方式匹配字符串的开始

match (n)
where n.name starts with 'Pet'
return n

Cypher学习《二》_正则表达式_10

匹配字符串的结尾
match (n)
where n.name ends with 'ter'
return n
字符串包含

contains用于检测字符串中是否包含某个字符串,它是大小写敏感的,而且不关心匹配部分在字符串中位置。

match (n)
where n.name contains 'ete'
return n

Cypher学习《二》_运算符_11

字符串反向匹配

使用not关键词可以返回不满足给定字符串匹配要求的结果

match (n)
where not n.name ends with 's'
return n
正则表达式

Cypher支持正则表达式过滤,正则表达式的语法继承来自Java正则表达式,它支持字符串如何匹配标记,包括不区分大小写 (?i), 多行 (?m) 和单行 (?s) 。

标记放在正则表达式的开头,例如 match (n) where ​​n.name​​ =~ '(?i)Lon.*' return n ,将返回名字为 London 和 LonDoN的节点可以使用=~ 'regexp' 来进行正则表达式的匹配

match (n)
where n.name =~ 'Tob.*'
return n

Cypher学习《二》_字符串_12

正则表达式中的转义字符
match (n)
where n.address =~ 'Sweden\\/MaImo'
return n

Cypher学习《二》_运算符_13

正则表达式非大小写敏感

在正则表达式前面加入(?i)之后,整个正则表达式将变成非大小写敏感

match (n)
where n.name =~ '(?i)ANDR.*'
return n

Cypher学习《二》_运算符_14

4.在where中使用路径模式

模式过滤,返回有外向关系指向 'Tobias' 的节点
match (tobias {name:'Tobias'}),(others)
where others.name in ['Andres','Peter'] and (tobias)<--(others)
return others

Cypher学习《二》_字符串_15

模式中的not过滤

not 功能可用于排除某个模式

返回没有外向关系指向‘Peter’的节点
match (persons),(peter {name:'Peter'})
where not (persons) --> (peter)
return persons

Cypher学习《二》_运算符_16

模式中属性过滤
match (n)
where (n)-[:knows]-({name:'Tobias'})
return n

Cypher学习《二》_运算符_17

关系类型过滤

这个查询返回已'Andres' 节点开始,以k开头的所有的关系

match (n)-[r]->()
where n.name = 'Andres' and type(r) =~ 'k.*'
return r

Cypher学习《二》_正则表达式_18

5.列表

in运算符

检查列表中是否存在某个元素,可以使用in运算符

match (a)
where a.name in ['Peter','Tobias']
return a
不存在的属性和值

如果属性不存在,对它的判断默认返回false

对于不存在的属性值就当做null,在下面例子中,对于没有belt属性的节点的比较将返回false

下面查询仅返回belt为white的节点

match (n)
where n.belt = 'white'
return n
属性不存在默认为true的情况

如果要比较属性存在,则可以与期望的直接进行比较。如果不存在,默认值为true

match (n)
where n.belt = 'white' or n.belt is null
return n
order by n.name

Cypher学习《二》_运算符_19

空值过滤
match (person)
where person.name = 'Peter' and person.belt is null
return person
使用范围

简单范围

检查某个元素是否在指定范围,可以使用不等运算符< ,>= 和 >

match (a)
where a.name >= 'Peter'
return a
范围的组合

多个不等式可以组合构成一个范围

match (a)
where a.name > 'Andres' and a.name < 'Tobias'
return a

6.start(次处后续和索引一同学习)

通过遗留索引查找开始点

Cypher中的每个查询描述了一个模式,一个模式可以有多个开始点。一个开始点是模式中的一个关系或者节点,使用start时,只能通过遗留索引寻找来引出开始点。

注意:使用一个不存在额遗留索引将报错,start语句应当仅用于访问遗留的索引,所有其他的情况,都应使用match代替

7.Aggregation

return n, count(*)

这里有两个表达式,n和count(),  前者n不是聚合函数,是一个分组键。后者count()是一个聚合函数。因此根据不同的分组键,匹配的子图将被分为不同的组,聚合函数将运行在这些组上来计算聚合值。

create (a:Person {name:'a',property:13})
create (b:Person {name:'b',property:33,eyes:'blue'})
create (c:Person {name:'c',property:44,eyes:'blue'})
create (d:Person {name:'d',eyes:'brown'})
create (e:Person {name:'d'})
create (a)-[:knows]->(b),
(a)-[:knows]->(c),
(a)-[:knows]->(d),
(b)-[:knows]->(e),
(c)-[:knows]->(e)

Cypher学习《二》_字符串_20

查找朋友的所有朋友并计算朋友的个数
match (me:Person)-->(friend:Person)-->(friend_of_friend:Person)
where me.name = 'a'
return count(distinct friend_of_friend),count(friend_of_friend)

Cypher学习《二》_正则表达式_21

计算节点

计算节点的数量。例如:如果要计算连接到某个节点的节点数,可用count(*)

match (a:Person {name:'a'})-->(x)
return a,count(*)

Cypher学习《二》_运算符_22

按组计算关系类型的数量

计算关系类型组中的数量,返回类型和数量

match (a:Person {name:'a'})-[r]->()
return type(r),count(*)

Cypher学习《二》_字符串_23

计算实体
match (a:Person {name:'a'})-->(x)
return count(x)
计算非空值数量
match (a:Person)
return count(a.property)

Cypher学习《二》_字符串_24

统计
sum

聚合函数sum,计算所有值之和,计算的时候空值将被丢弃

match (a:Person)
return sum(a.property)

Cypher学习《二》_字符串_25

avg

avg计算数值列的平均值

match (a:Person)
return avg(a.property)
percentileDisc

percentileDisc计算给定值在一个组中的百分比,取值从0.0到1.0。它使用舍入法,返回最接近百分位的值,对应插值法,请参考percentileCont函数

match (a:Person)
return percentileDisc(a.property,0.5)

Cypher学习《二》_正则表达式_26

percentileCont

percentileCont计算给定值在一个组中的百分位,百分位的值从0.0到1.0,它采用线性插值法,在两值之间计算一个加权平均数。

match (a:Person)
return percentileCont(a.property,0.4)

Cypher学习《二》_正则表达式_27

stdev

stdev计算给定值在一个组中的标准偏差,它采用标准的two-pass方法,以N-1作为分母,当以部分样本作为无偏估计时应使用stdev;当计算整个样本的标准偏差时,应该使用stdevp

match (a:Person)
where a.name in ['a','b','c']
return stdev(a.property)

Cypher学习《二》_字符串_28

stdevp

stdevp计算给定值在一个组中的标准偏差

match (n)
where n.name in ['a','b','c']
return stdevp(n.property)

Cypher学习《二》_字符串_29

max

max查找数值列中的最大值

match (n:Person)
return max(n.property)

Cypher学习《二》_正则表达式_30

min

min查找数值列中的最小值

match (n:Person)
return min(n.property)

Cypher学习《二》_正则表达式_31

collect

collect将所有的值收集起来放在一个列表中,空值null将被忽略

match (n:Person)
return collect(n.property)

Cypher学习《二》_正则表达式_32

distinct

match (a:Person {name:'a'})-->(b)
return count(distinct b.eyes)

Cypher学习《二》_正则表达式_33

【本文来自:美国大带宽服务器 http://www.558idc.com/mg.html提供,感恩】
上一篇:Zabbix 6 系列学习 08:组件分离式部署
下一篇:没有了
网友评论