Search code examples
neo4jcyphersocial-networkingexcept

Please Help Me Use "EXCEPT Clause" in "Neo4j"


I have some nodes in Neo4j, with friendship relation. Now I want to query for friends of friends of one Node(ex:Kate), except the nodes that are already friends with her. I tried this commands, but it doesn't work:

MATCH ( p:person {name:"Kate"} )-[friends_with*2..3] -> (pp:person)
WHERE (pp) NOT (p)->[friends_with]-(pp)
return pp;

OR

  MATCH ( p:person {name:"Kate"} )-[friends_with*2..3] -> (pp:person)
    WHERE (pp) OUT [(p)->[friends_with]-(pp)]
    return pp;

I will appreciate if any one could help


Solution

  • Almost there, just syntax:

    MATCH (p:person {name:"Kate"})-[:friends_with*2..3]->(pp:person)
    WHERE NOT ((p)-[:friends_with]->(pp))
    return pp;
    

    BTW, you've considered the direction as well. So if (pp)-[:friends_with]->(p), it won't be excluded. If that was not the intention then just leave off the directions.