Neo4j排序由谁跟随我,跟随我,其他人

时间:2014-04-27 09:42:02

标签: neo4j cypher

让我们假设一个社交网络

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?

1 个答案:

答案 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。