Neo4J:好友推荐简单示例

2019-10-22 12:04:12 浏览数 (2)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/chengyuqiang/article/details/102658749

说明:该例子来源于《Neo4j 3.x 入门经典》

1、初始化数据

代码语言:javascript复制
CREATE (leBig:Dude {name: "Lebowski"})
CREATE (dudeA1:Dude {name: "A1"})
CREATE (dudeA2:Dude {name: "A2"})

CREATE (leBig)<-[:FRIEND_OF]-(dudeA1)
CREATE (leBig)<-[:FRIEND_OF]-(dudeA2)

CREATE (dudeB1:Dude {name: "B1"})
CREATE (dudeB2:Dude {name: "B2"})
CREATE (dudeB3:Dude {name: "B3"})
代码语言:javascript复制
MATCH (dudeA1:Dude {name: "A1"})
MATCH (dudeA2:Dude {name: "A2"})
MATCH (dudeB1:Dude {name: "B1"})
MATCH (dudeB2:Dude {name: "B2"})
MATCH (dudeB3:Dude {name: "B3"})
CREATE (dudeA1)<-[:FRIEND_OF]-(dudeB1)
CREATE (dudeA1)<-[:FRIEND_OF]-(dudeB2)
CREATE (dudeA1)<-[:FRIEND_OF]-(dudeB3)
CREATE (dudeA2)<-[:FRIEND_OF]-(dudeB1)
CREATE (dudeA2)<-[:FRIEND_OF]-(dudeB2)
CREATE (dudeA2)<-[:FRIEND_OF]-(dudeB3)

CREATE (dudeC1:Dude {name: "C1"})
CREATE (dudeC2:Dude {name: "C2"})
CREATE (dudeC3:Dude {name: "C3"})
CREATE (dudeC4:Dude {name: "C4"})

CREATE (dudeB1)<-[:FRIEND_OF]-(dudeC1)
CREATE (dudeB1)<-[:FRIEND_OF]-(dudeC2)
CREATE (dudeB1)<-[:FRIEND_OF]-(dudeC3)
CREATE (dudeB1)<-[:FRIEND_OF]-(dudeC4)

CREATE (dudeB2)<-[:FRIEND_OF]-(dudeC1)
CREATE (dudeB2)<-[:FRIEND_OF]-(dudeC2)
CREATE (dudeB2)<-[:FRIEND_OF]-(dudeC3)
CREATE (dudeB2)<-[:FRIEND_OF]-(dudeC4)

CREATE (dudeB3)<-[:FRIEND_OF]-(dudeC1)
CREATE (dudeB3)<-[:FRIEND_OF]-(dudeC2)
CREATE (dudeB3)<-[:FRIEND_OF]-(dudeC3)
CREATE (dudeB3)<-[:FRIEND_OF]-(dudeC4)
代码语言:javascript复制
MATCH (d:Dude) RETURN d

2、好友推荐

(1) Lebowski的朋友

代码语言:javascript复制
MATCH (le:Dude {name:"Lebowski"})<-[:FRIEND_OF]-(some:Dude)
RETURN some

(2) Lebowski的朋友的朋友

代码语言:javascript复制
MATCH (le:Dude {name:"Lebowski"})<-[:FRIEND_OF]-(someA:Dude)<-[:FRIEND_OF]-(someB:Dude)
RETURN someB

(3) Lebowski的朋友的朋友的朋友

代码语言:javascript复制
MATCH (le:Dude {name:"Lebowski"})<-[:FRIEND_OF*3]-(some:Dude)
RETURN DISTINCT some
代码语言:javascript复制
MATCH (le:Dude {name:"Lebowski"})<-[:FRIEND_OF*3]-(some:Dude) RETURN some

0 人点赞