1.1 Cypher
1.1.1 概念
Neo4j中不存在表的概念,只有两类:节点(Node)和关联(Relation),可以简单理解为图里面的点和边。
1.1.2 CREATE
新建节点-标签-关系
CREATE
(pll:TEST1 {name:'森林',mj:"5000亩"}),
(pl2:TEST1 {name:'沙漠',mj:"3000亩"}),
(lb1:TEST2 {name:'大树'}),
(lb2:TEST2 {name:'小树'}),
(lb3:TEST2 {name:'杂草'}),
(zll:TEST3 {name:'灌木'}),
(zl2:TEST3 {name:'乔木'}),
(lb1)-[:zb{bl:"60%",mj:"3000亩"}]->(pll),
(lb2)-[:zb{bl:"30%",mj:"2000亩"}]->(pll),
(lb3)-[:zb{bl:"10%",mj:"1000亩"}]->(pll),
(lb1)-[:zb{bl:"10%",mj:"100亩"}]->(pl2),
(lb2)-[:zb{bl:"20%",mj:"900亩"}]->(pl2),
(lb3)-[:zb{bl:"70%",mj:"2000亩"}]->(pl2),
(lb3)-[:zb{bl:"70%",mj:"2000亩"}]->(pl2),
(zll)-[:zb{bl:"70%",mj:"1000亩"}]->(pl1)
新建节点
CREATE (pl3:TEST1 {name:'海洋',mj:"5000亩"})
新建标签
CREATE (pll:TEST3 {name:'灌木'})
新建关系属性
MATCH (x:TEST1),(y:TEST2)
WHERE x.name="海洋" and y.name="杂草"
CREATE (y)-[:zb{bl:"1%",mj:"88亩"}]->(x)
RETURN x,y;
1.1.3 MATCH
查询整体
MATCH (a:TEST1),(b:TEST2) RETURN a,b
查询森林的植物
MATCH (a:TEST1),(b:TEST2)
WHERE a.name = "森林"
RETURN a,b
查询沙漠中面积为100亩的植物
match (a:TEST1)<-[r]-(b:TEST2)
where a.name = "沙漠" and r.mj='100亩'
return a,b;
with -查询标签一与标签二有关还同时与标签三有关的信息
MATCH (x:TEST1)<-[r]-(y:TEST2 { name: "大树"})
WITH x
MATCH (x)<-[r]-(z:TEST3)
RETURN x,z
Union/union all-查询标签一与标签二有关和标签一与标签三有关的并集
MATCH (x:TEST1)<-[r]-(y:TEST2)
RETURN x.name
UNION ALL
MATCH (x:TEST1)<-[r]-(z:TEST2)
RETURN x.name
字符串匹配
字符串首尾可以使用STARTS WITH 和 ENDS WITH,去匹配定位字符串,字符串匹配还可以使用CONTAINS来匹配字符串中包含的自字符串。
MATCH (x:TEST1) WHERE x.name STARTS WITH '沙' RETURN x
MATCH (x:TEST1) WHERE x.name ENDS WITH '漠' RETURN x
MATCH (x:TEST1) WHERE x.name CONTAINS '漠' RETURN x
字符串排除匹配
在字符串匹配的时候可以使用NOT关键字来排除匹配到的结果,得到相反的结果
MATCH (x:TEST1) WHERE NOT x.name STARTS WITH '沙' RETURN x
字符串正则表达式,类似SQL中的like ‘%漠%’
MATCH (x:TEST1) WHERE x.name =~ '.*漠.*' RETURN x
匹配分页返回
MATCH (x:TEST1) RETURN x SKip 1 LIMIT 2
匹配去重返回
MATCH (x:TEST1) RETURN DISTINCT x
匹配分组返回值
MATCH (x:TEST1) RETURN ORDER BY x.name desc
别名返回
MATCH (x:TEST1) RETURN x.name as xname
1.1.4 SET
修改节点属性值
MATCH (x:TEST1)
WHERE x.name = "沙漠"
SET x.name = "荒漠"
修改关系属性
match (x:TEST1)<-[r]-(y:TEST2)
where x.name = "沙漠" and y.name='大树'
set r.bl='80%'
return x,y;
1.1.5 DELETE
删除标签
MATCH (x:TEST3) DELETE x
删除节点-(与其他节点不存在关系)
MATCH (a:TEST1)
WHERE a.name = "森林"
DELETE a
删除节点-(与其他节点存在关系)
MATCH (x:TEST1)<-[r]-(y:TEST2)
WHERE x.name = "森林"
DELETE x,y
删除节点和关系
MATCH (x:movie)-[r]-(y:Person) RETURN r
MATCH (x:movie)-[r]-(y:Person) DELETE x,y,r
删除关系属性
match (x:TEST1)<-[r]-(y:TEST2)
where x.name = "沙漠" and y.name='大树'
delete r;
1.1.6 INDEX
创建索引:为” TEST1”标签的name属性创建索引
CREATE INDEX ON :TEST1(name)
删除索引:删除” TEST1”标签的name属性的索引
DROP INDEX ON : TEST1(name)
匹配条件中使用索引
MATCH (n: TEST1) WHERE n.name = '森林' RETURN n
创建节点属性唯一约束
创建一个唯一约束保证图数据库中同一个标签内的节点的属性值不重复。
注意:不可以为已经创建索引的属性创建唯一约束
CREATE CONSTRAINT ON (n:TEST1) ASSERT n.name IS UNIQUE
删除创建节点属性唯一约束
DROP CONSTRAINT ON (n:TEST1) ASSERT n.name IS UNIQUE
标签:TEST1,Cypher,RETURN,name,标签,mj,Neo4j,MATCH
From: https://blog.csdn.net/qq_45796594/article/details/140134986