需要造一笔数据,用来查询用:
create (p:person{name:"宋江",sex:"男",age:"38",hobby:"权力",nickName:"及时雨、呼保义、孝义黑三郎、宋公明、宋押司"}); create (p:person{name:"宋太公",sex:"男",age:"68",hobby:"",nickName:""}); create (p:person{name:"扈三娘",sex:"女",age:"24",hobby:"",nickName:"一丈青"}); create (p:person{name:"王英",sex:"男",age:"37",hobby:"",nickName:"矮脚虎"}); create (p:person{name:"阎婆惜",sex:"女",age:"22",hobby:"",nickName:""}); create (p:person{name:"晁盖",sex:"男",age:"39",hobby:"",nickName:"托塔天王"}); create (p:person{name:"武松",sex:"男",age:"33",hobby:"",nickName:"行者、武二郎"}); create (p:person{name:"武大郎",sex:"男",age:"40",hobby:"",nickName:""}); create (p:person{name:"潘金莲",sex:"女",age:"24",hobby:"",nickName:""}); create (p:person{name:"乔郓哥",sex:"男",age:"13",hobby:"",nickName:"小猴子"}); create (p:person{name:"王婆",sex:"女",age:"56",hobby:"",nickName:""}); create (p:person{name:"西门庆",sex:"男",age:"34",hobby:"",nickName:"西门大官人"}); create (p:person{name:"玉兰",sex:"女",age:"18",hobby:"",nickName:""}); match (p1:person{name:"宋江"}),(p2:person{name:"宋太公"}) create (p1)-[r:父亲]->(p2) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"宋太公"}) create (p2)-[r:儿子]->(p1) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"扈三娘"}) create (p1)-[r:义妹]->(p2) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"扈三娘"}) create (p2)-[r:义兄]->(p1) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"阎婆惜"}) create (p1)-[r:小妾]->(p2) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"阎婆惜"}) create (p2)-[r:夫君]->(p1) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"王英"}) create (p1)-[r:小弟]->(p2) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"王英"}) create (p2)-[r:哥哥]->(p1) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"晁盖"}) create (p1)-[r:哥哥]->(p2) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"晁盖"}) create (p2)-[r:小弟]->(p1) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"武松"}) create (p1)-[r:小弟]->(p2) return p1,p2; match (p1:person{name:"宋江"}),(p2:person{name:"武松"}) create (p2)-[r:哥哥]->(p1) return p1,p2; match (p1:person{name:"扈三娘"}),(p2:person{name:"王英"}) merge (p1)-[r:夫君]->(p2) return p1,p2; match (p1:person{name:"扈三娘"}),(p2:person{name:"王英"}) merge (p2)-[r:妻子]->(p1) return p1,p2; match (p1:person{name:"晁盖"}),(p2:person{name:"武松"}) merge (p1)-[r:小弟]->(p2) return p1,p2; match (p1:person{name:"晁盖"}),(p2:person{name:"武松"}) merge (p2)-[r:哥哥]->(p1) return p1,p2; match (p1:person{name:"晁盖"}),(p2:person{name:"王英"}) merge (p1)-[r:小弟]->(p2) return p1,p2; match (p1:person{name:"晁盖"}),(p2:person{name:"王英"}) merge (p2)-[r:哥哥]->(p1) return p1,p2; match (p1:person{name:"武松"}),(p2:person{name:"武大郎"}) merge (p1)-[r:亲哥]->(p2) return p1,p2; match (p1:person{name:"武松"}),(p2:person{name:"武大郎"}) merge (p2)-[r:亲弟]->(p1) return p1,p2; match (p1:person{name:"武松"}),(p2:person{name:"潘金莲"}) merge (p1)-[r:嫂子]->(p2) return p1,p2; match (p1:person{name:"武松"}),(p2:person{name:"潘金莲"}) merge (p2)-[r:小叔子]->(p1) return p1,p2; match (p1:person{name:"玉兰"}),(p2:person{name:"武松"}) merge (p1)-[r:喜欢]->(p2) return p1,p2; match (p1:person{name:"武松"}),(p2:person{name:"西门庆"}) merge (p1)-[r:仇敌]-(p2) return p1,p2; match (p1:person{name:"武大郎"}),(p2:person{name:"潘金莲"}) merge (p1)-[r:妻子]->(p2) return p1,p2; match (p1:person{name:"武大郎"}),(p2:person{name:"潘金莲"}) merge (p2)-[r:夫君]->(p1) return p1,p2; match (p1:person{name:"武大郎"}),(p2:person{name:"乔郓哥"}) merge (p1)-[r:朋友]-(p2) return p1,p2; match (p1:person{name:"武大郎"}),(p2:person{name:"王婆"}) merge (p1)-[r:邻居]-(p2) return p1,p2; match (p1:person{name:"潘金莲"}),(p2:person{name:"王婆"}) merge (p1)-[r:邻居]-(p2) return p1,p2; match (p1:person{name:"潘金莲"}),(p2:person{name:"西门庆"}) merge (p1)-[r:情人]-(p2) return p1,p2; match (p1:person{name:"西门庆"}),(p2:person{name:"武大郎"}) merge (p1)-[r:杀死]->(p2) return p1,p2; match (p1:person{name:"西门庆"}),(p2:person{name:"王婆"}) merge (p1)-[r:认识]-(p2) return p1,p2;
最短路径查询:宋江到王英的最短路径:
match (p1:person{name:"宋江"}),(p2:person{name:"王英"}),p=shortestpath((p1)-[*..10]-(p2)) return p //p 表示路径 //(p1)-[*..10]-(p2) 表示10层以内
结果:
查询所有最短路径:
match (p1:person{name:"宋江"}),(p2:person{name:"王英"}),p=((p1)-[*1..3]->(p2)) return p
结果:
注意:环路还包含了自己。
查询含有某种关系的节点数目
match (p1:person)-[r:小弟]->() return count(p1)