让我们假设一个社交网络
ne4j控制台的代码:http://console.neo4j.org/?id=fij8qs
CREATE (p1:PERSON {name: "Edson"})
CREATE (p2:PERSON {name: "Clades"})
CREATE (I:PERSON {name: "Mateus"})
CREATE (other:PERSON {name: "Goku"})
CREATE (alb:ALBUM {name: "Férias"})
CREATE I-[:HAVE_ALBUM]->alb
CREATE I-[:FOLLOW]->p1
CREATE p1-[:LIKE]->alb
CREATE p2-[:LIKE]->alb
CREATE other-[:LIKE]->alb
如何: 选择喜欢相册的所有人。 通过订购 之后由朋友的朋友然后。
选择所有[:LIKE]我的专辑ORDER BY
I-[:FOLLOW]
p2-[:FOLLOW]->person-[:LIKE]->alb // friend of my friend
person-[:LIKE]->alb //other
类似的问题 [Cypher: Is it possible to find creepy people following my friends?
答案 0 :(得分:2)
您需要使用可变长度的可选匹配来了解您与相册之间的距离,然后按距离排序:
MATCH (person)-[:LIKE]->(:ALBUM {name:"Férias"}), (me:PERSON {name:"Mateus"})
OPTIONAL MATCH p=(me)-[:FOLLOW*]->(person)
WITH person, coalesce(length(p), 9999) AS distance
RETURN person, distance
ORDER BY distance
coalesce
为您不直接或间接关注的所有人分配了高值9999。