cypher语法
cypher查询语句既可以在cypher-shell中使用,也可以在python中使用。
在python中使用:
from py2neo import Graph
g = Graph(host='127.0.0.1', auth=('neo4j', 'neo4j'))
sql = "MATCH p=(s)-[r]->(o) where s.name='普通感冒' and labels(s)=['疾病'] and type(r)="病因" RETURN s.name, type(r), o.name, labels(o) limit 3"
res = g.run(sql)
In [15]: type(res) # 查询结果得到一个游标,可以通过for或者next进行遍历取得每条结果数据,或者可以转成其他格式数据
Out[15]: py2neo.database.work.Cursor
In [12]: res.data() # data函数可以转化成一个list,list的每个元素为一个dict,dict的key即为sql中返回的字段
Out[12]:
[{'s.name': '普通感冒',
'type(r)': '病因',
'o.name': '病毒感染',
'labels(o)': ['disease']},
{'s.name': '普通感冒',
'type(r)': '病因',
'o.name': '鼻病毒',
'labels(o)': ['disease']},
{'s.name': '普通感冒', 'type(r)': '病因', 'o.name': '冠状病毒', 'labels(o)': ['病原体']}]
In [16]: res.to_data_frame() # to_data_frame函数可以转化成dataframe
Out[16]:
s.name type(r) o.name labels(o)
0 普通感冒 病因 病毒感染 [disease]
1 普通感冒 病因 鼻病毒 [disease]
2 普通感冒 病因 冠状病毒 [病原体]
Out[5]:
s.name | type(r) | o.name | labels(o)
--------|---------|--------|------------------------
普通感冒 | 病因 | 病毒感染 | ['disease']
普通感冒 | 病因 | 鼻病毒 | ['disease']
普通感冒 | 病因 | 冠状病毒 | ['\u75c5\u539f\u4f53']
Input:
MATCH(n) RETURN count(n); # 统计节点数量
Output:
+----------+
| count(n) |
+----------+
| 26220 |
+----------+
Input:
MATCH p=(s)-[r]->(o) RETURN count(p); # 统计关系的数量
Output:
+----------+
| count(p) |
+----------+
| 39284 |
+----------+
match (n) detach delete n; # 删除所有节点
查询操作
Input:
MATCH p=(s)-[r]->(o) RETURN p LIMIT 10; # 查询所有关系
Output:
+---------------------------------------------------------------+
| p |
+---------------------------------------------------------------+
| (:疾病 {name: "血吸虫病"})-[:疾病所需检查]->(:检查 {name: "血细胞计数"}) |
| (:疾病 {name: "慢性淋巴细胞白血病"})-[:疾病所需检查]->(:检查 {name: "血细胞计数"}) |
| (:疾病 {name: "肾小球肾炎"})-[:疾病所需检查]->(:检查 {name: "血细胞计数"}) |
| (:疾病 {name: "类癌综合征"})-[:疾病所需检查]->(:检查 {name: "血细胞计数"}) |
| (:疾病 {name: "鞭虫病"})-[:鉴别诊断]->(:疾病 {name: "钩虫病"}) |
| (:疾病 {name: "hookworm diseases"})-[:同义词]->(:疾病 {name: "钩虫病"}) |
| (:疾病 {name: "钩虫病"})-[:并发症]->(:疾病 {name: "失血性贫血"}) |
| (:疾病 {name: "先天性甲低"})-[:临床表现]->(:症状 {name: "食欲缺乏"}) |
| (:疾病 {name: "低血糖"})-[:临床表现]->(:症状 {name: "食欲缺乏"}) |
| (:疾病 {name: "新生儿呕吐"})-[:临床表现]->(:症状 {name: "食欲缺乏"}) |
+---------------------------------------------------------------+
Input:
MATCH p=(s:疾病{name:'普通感冒'})-[r:病因]->(o) RETURN s.name, type(r), o.name, labels(o); # 返回sub的名字、关系的名字、obj的名字和类型
MATCH p=(s)-[r]->(o) where s.name="普通感冒" and labels(s)=['疾病'] and type(r)="病因" RETURN s.name, type(r), o.name, labels(o);
Output:
+-----------------------------------------------+
| s.name | type(r) | o.name | labels(o) |
+-----------------------------------------------+
| "普通感冒" | "病因" | "病毒感染" | ["disease"] |
| "普通感冒" | "病因" | "鼻病毒" | ["disease"] |
| "普通感冒" | "病因" | "冠状病毒" | ["病原体"] |
| "普通感冒" | "病因" | "副流感病毒" | ["disease"] |
| "普通感冒" | "病因" | "呼吸道合胞病毒" | ["check_item"] |
| "普通感冒" | "病因" | "埃可病毒" | ["病原体"] |
| "普通感冒" | "病因" | "柯萨奇病毒" | ["病原体"] |
| "普通感冒" | "病因" | "冠状病毒" | ["致病因素"] |
| "普通感冒" | "病因" | "流感病毒" | ["disease"] |
| "普通感冒" | "病因" | "偏肺病毒" | ["致病因素"] |
+-----------------------------------------------+
Input:
MATCH p=(s:`疾病`{name: '普通感冒'})-[r:`临床表现`{inStage:'发病同时或数小时后'}]->(o) RETURN o.name; # 普通感冒指定发病时间段时的症状
MATCH p=(s:`疾病`{name: '普通感冒'})-[r:`临床表现`]->(o) where r.inStage='发病同时或数小时后' RETURN o.name;
Output:
+----------+
| o.name |
+----------+
| "喷嚏" |
| "鼻塞" |
| "流清水样鼻涕" |
+----------+
Input:
MATCH (s)-[r]->(o) where type(r)=~'.*治疗' and s.name='普通感冒' return s.name, type(r), o.name; # 模糊查询
Output:
+--------------------------------+
| s.name | type(r) | o.name |
+--------------------------------+
| "普通感冒" | "药物治疗" | "解热镇痛剂" |
| "普通感冒" | "药物治疗" | "抗组胺剂" |
| "普通感冒" | "药物治疗" | "氢可酮" |
| "普通感冒" | "药物治疗" | "可待因" |
| "普通感冒" | "药物治疗" | "含阿片类药物" |
| "普通感冒" | "药物治疗" | "布洛芬" |
| "普通感冒" | "药物治疗" | "对乙酰氨基酚" |
| "普通感冒" | "药物治疗" | "阿司匹林" |
| "普通感冒" | "药物治疗" | "镇痛药的联合治疗" |
| "普通感冒" | "药物治疗" | "减充血剂" |
| "普通感冒" | "药物治疗" | "含抗组胺药" |
| "普通感冒" | "药物治疗" | "西替利嗪" |
| "普通感冒" | "药物治疗" | "伪麻黄碱" |
| "普通感冒" | "药物治疗" | "异丙托溴铵" |
| "普通感冒" | "药物治疗" | "羟甲唑啉" |
| "普通感冒" | "药物治疗" | "可待因和氢可酮" |
| "普通感冒" | "药物治疗" | "阿片类药物" |
| "普通感冒" | "药物治疗" | "藿香正气滴丸" |
| "普通感冒" | "药物治疗" | "非洲天竺葵" |
| "普通感冒" | "药物治疗" | "补充维生素 C" |
| "普通感冒" | "药物治疗" | "维生素 C" |
| "普通感冒" | "药物治疗" | "抗生素" |
| "普通感冒" | "其他治疗" | "疫苗" |
| "普通感冒" | "其他治疗" | "维持足够的液体摄入" |
| "普通感冒" | "其他治疗" | "休息" |
| "普通感冒" | "其他治疗" | "针灸和艾灸" |
| "普通感冒" | "其他治疗" | "艾灸" |
| "普通感冒" | "其他治疗" | "针灸" |
+--------------------------------+