Fw: [Neo4j] Cypher Query Filtering Friends of Friends nodes

110 views
Skip to first unread message

Chris Bolton

unread,
Jul 31, 2012, 5:34:54 PM7/31/12
to ne...@googlegroups.com
Okay I started tinkering with this the console (http://console.neo4j.org/r/glfxa4) more and came up with this:

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

It returns the starting node and the desired result node. Is there something obvious I'm missing?

Chris 

--- On Tue, 7/31/12, Chris Bolton <iknewt...@yahoo.com> wrote:

From: Chris Bolton <iknewt...@yahoo.com>
Subject: [Neo4j] Cypher Query Filtering Friends of Friends nodes
To: ne...@googlegroups.com
Date: Tuesday, July 31, 2012, 2:26 PM

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:41:16 PM7/31/12
to ne...@googlegroups.com
No, thats pretty good, until cypher gets some more functiions/operators to work with paths.

Is this the depth you need in the end?

Michael

Chris Bolton

unread,
Jul 31, 2012, 5:43:15 PM7/31/12
to ne...@googlegroups.com
I realized after I sent the initial email that I copied my query from my code into the email which uses lower case as opposed to the console where I used upper case (don't ask me why). Sorry about that.

What about removing the initial node in my results? 

That's the depth I need. Thanks Michael.

--- On Tue, 7/31/12, Michael Hunger <michael...@neotechnology.com> wrote:

Michael Hunger

unread,
Jul 31, 2012, 6:24:36 PM7/31/12
to ne...@googlegroups.com
What do you mean by removing the initial node?

Michael

Chris Bolton

unread,
Jul 31, 2012, 6:26:24 PM7/31/12
to ne...@googlegroups.com
http://console.neo4j.org/?id=elexsz

This returns z, which results in two rows, the reference node and the result node. How do I get it to omit the reference node?

Michael Hunger

unread,
Jul 31, 2012, 6:36:58 PM7/31/12
to ne...@googlegroups.com
add an 

AND z<>n

in your where clause.

Michael

Chris Bolton

unread,
Jul 31, 2012, 6:36:59 PM7/31/12
to ne...@googlegroups.com

Chris Bolton

unread,
Aug 1, 2012, 1:35:35 PM8/1/12
to ne...@googlegroups.com
Michael (or anyone else who might be able to help),

I added the z<>n and now it only returns the reference node...twice.

In the console it works perfectly but in my Neo4j shell it's breaking (http://console.neo4j.org/?id=elexsz).

START n=node(4) MATCH (n)-[:FRIENDS]->(x)-[:FRIENDS]->(z) WHERE (x-[:FRIENDS]->n AND z-[:FRIENDS]->x) AND NOT(n-[:BLOCKED_FRIENDS]->x AND x-[:BLOCKED_FRIENDS]->z) AND z<>n RETURN z

I'm using Neo4j - Graph Database Kernel 1.7

Any help would be appreciated.

Chris 

Michael Hunger

unread,
Aug 1, 2012, 3:57:57 PM8/1/12
to ne...@googlegroups.com
can you put n<>z at the beginning of the where

in 1.7 there was an issue with NOT and parentheses which grabbed too much of an expression.

Michael

Chris Bolton

unread,
Aug 1, 2012, 4:20:30 PM8/1/12
to ne...@googlegroups.com
Michael,

Okay the z <> n is working but I realized my blocked_friends is not working. I tried re-writing the query with your original suggestion of using WITH but I don't think it's supported in 1.7. Mind taking a look. Again it works in the play console but in the shell it breaks.

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

Chris

Michael Hunger

unread,
Aug 1, 2012, 5:02:00 PM8/1/12
to ne...@googlegroups.com
No with is not supported in 1.7
and the NOT() issue was also only fixed in 1.8

Sorry

Michael

Michael Hunger

unread,
Aug 1, 2012, 5:02:21 PM8/1/12
to ne...@googlegroups.com
Btw. you can set the console to a certain version in the upper right corner.

Michael

Am 01.08.2012 um 22:20 schrieb Chris Bolton:

Chris Bolton

unread,
Aug 1, 2012, 5:07:27 PM8/1/12
to ne...@googlegroups.com
Thanks Michael.
Reply all
Reply to author
Forward
0 new messages