Cypher Query Filtering Friends of Friends nodes

18 views
Skip to first unread message

Chris Bolton

unread,
Jul 31, 2012, 5:26:43 PM7/31/12
to ne...@googlegroups.com
Afternoon all,

I'm looking for some help writing a query.

I've built this query so that it finds all friends that have a reciprocated relationship and are not blocked friends.

START n=node(1) MATCH (n)-[:friends]->(x) WHERE (x-[:friends]->n) AND NOT(n<-[:blocked_friends]-x) RETURN x

I'm now trying to go one layer deeper and pull the friends of friends filtered by the same criteria on the first level and the second level.

I set up the Neo4j console (http://console.neo4j.org/?id=j6cyp4in hopes that it would be easier to visualize. I am trying to return just Mike starting with Bob.  

Any help would be greatly appreciated.

Thanks.
Chris

Michael Hunger

unread,
Jul 31, 2012, 5:40:11 PM7/31/12
to ne...@googlegroups.com
Chris 

the simplest way to do that which comes to my mind is just to use WITH and repeat the query one step deeper

btw. it took me a while to realize that you spelled the rel-types uppercase in the graph setup but lowercase in the query, don't do that, they are case-sensitive !

START n=node(1) MATCH (n)-[:FRIENDS]->(m) WHERE (m-[:FRIENDS]->n) AND NOT(n-[:BLOCKED_FRIENDS]->m) WITH m MATCH (m)-[:FRIENDS]->(x) WHERE (x-[:FRIENDS]->m) AND NOT(m-[:BLOCKED_FRIENDS]->x) RETURN x

for arbitrary length matches it would be necessary to use a variable length path and check  each pair of nodes, which is right now not supported in cypher (afaik).
or at least have a function to return the start and endnode of a relationship.

Reply all
Reply to author
Forward
0 new messages