Question

Let's suppose a social network

Code at ne4j console: 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

HOW: select all people who likes the album. order by my friends after by friends of my friends, then by any other.

Select all that [:LIKE] my album ORDER BY

I-[:FOLLOW]
p2-[:FOLLOW]->person-[:LIKE]->alb // friend of my friend
person-[:LIKE]->alb //other

similar question [Cypher: Is it possible to find creepy people following my friends?

Was it helpful?

Solution

You need to use an optional match with variable length to know the distance between you and the likers of the album and then sort by distance:

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

The coalesce assigns a high value of 9999 to everyone you don't follow directly or indirectly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top