1. 根据节点属性查找对应节点:
其他例子:
match(x:Student{studentId:'1001'}) return x 或者
match(x:Student) where x.studentId='1001' return x
我的例子:
2. 根据关系查找节点
其他例子:
match (x)-[r:教学内容]-(y) where r.课程='语文' return x,r,y
我的例子:
match p=(n:`药品`)-[r:`诊断禁忌`]-(d:`诊断`{name:'痛风'}) return p limit 25
用p来代表一个三元组,return p等价于 return x, r, y
3. 查询单独的节点,即:与其他任何节点没有任何关系
match(x) where not (x)-[]-() return x
4. 查询N层关系的节点:
其他例子:
match q=(x)-[*5..8]-() return q limit 200 这个为查询5到8层关系的
match q=(dh)-[r]-(jq)-[rr]-()-[]-()-[]-()-[]-()-[]-()-[]-() return q limit 400
我的例子:
这个不太好理解,我们可以对比着看:
1层关系
1-2层关系:
1-3层关系:
*1..N的意思即为:如果在1到N的关系中存在路径,将返回开始点和结束点。
这种查询对有方向的关系比较实用,我这个例子都是一层的关系,所以查询多层关系时,关系的方向不一致,查出来的意思不大。
如果加上方向,即查询有3到5层关系的节点,结果就有点用了:
5. 查询节点关系数个数:
其他例子:
match(dh:`学生`)-[r]-(jq:`老师`) with dh, count(r) as dhs where dhs > 2 return dh
我的例子:
查询禁忌症多于2个的药物,并展示药物及其禁忌的关系:
with dh, count(jq) as dhs where dhs > 2,
这条语句只能返回满足条件的一个字段:dh,然后再用这个dh再去match,关联上其他的字段进行搜索。
6. 查询节点个数:
match(x) return count(x)
7. 查询所有的关系类型:
CALL db.relationshipTypes()
8. 查询所有的节点标签:
CALL db.labels()
9. 查询节点关系种类:
CALL db.schema()
10. 最短路径查询
MATCH (x:电话{hm:"02711111111"}),(y:电话{sjdbh:"025111111111"}),p=shortestpath((x)-[*..10]-(y))RETURN p
11. 查询不存在某个属性的节点
match(x:电话) where x.repeat is null with x match p=(x)-[r*1..5]-(y) return p
12. 查询存在关联到某一个节点具有相同属性的其他的节点
其他例子:
match p=(x)-[]-()-[]-(y) where x.name=y.name return p
我的例子:
13. 查询两个节点之间存在多个关系的节点
其他例子:
match p=(x)-[r1]-(y)-[r2]-(x) where id(r1)<>id(r2) return p
我的例子:
查询两种药物之间有两个不同关系。
14. 查询某一个节点具有m到n层关系的所有的节点
match q=(x:学生)-[*1..5]-() where x.no ='201921011XXXX' return q #查询某学生有关系的节点,最多五层关系
15. 用not过滤关系
其他例子:
MATCH (p:Person)-[:ACTED_IN]->(m)
WHERE NOT (p)-[:DIRECTED]->()
RETURN p,m
我的例子:
16. 对结果排序
17. 创建索引
代码:
index_str='create index on:药品(genId)'
graph.run(index_str)
验证:
18. 创建唯一值约束
index_str='create constraint on (m:`ICD10编码`) assert m.name is unique'
graph.run(index_str)
验证:
19. 模糊查询