[Neo4j] [Cypher] Retrieve all nodes which are not related to a specific node

84 views
Skip to first unread message

Marc de Verdelhan

unread,
May 7, 2012, 12:04:02 PM5/7/12
to ne...@googlegroups.com
Hello,

I have the following graph: http://tinyurl.com/893tzu5

Then when I execute this query: START gp=node(6) MATCH gp-[:B]->c<-[:B]-mp<-[:SW|G]-u WHERE u.name <> "u1" RETURN mp
... it returns p1 and p2.

Could you explain why I'm getting "p1"?
Or in other terms: I would like to remove p1 from these results because this node is related to a node whose "name" property is "u1".

Cheers,

Marc

Michael Hunger

unread,
May 7, 2012, 12:19:37 PM5/7/12
to ne...@googlegroups.com
b/c there are two paths along which you can reach u1 and you output all the paths.

Michael

Marc de Verdelhan

unread,
May 7, 2012, 3:26:46 PM5/7/12
to ne...@googlegroups.com
Ok. I wasn't thinking in graph traversal way.

So now, as I tried to do with my query: starting from gw1, I want to retrieve all the nodes which are related to c1 except those which are related to at least one node whose the "name" property equals "u1". What would be the query for that?

Cheers,

Marc

Michael Hunger

unread,
May 7, 2012, 5:50:02 PM5/7/12
to ne...@googlegroups.com
Marc,

the best I came up with is:
START gp=node(6) MATCH gp-[:B]->c<-[:B]-mp<-[:SW|G]-u with mp, collect(u) as us where NONE(n in us where n.name ='u1') return mp


But perhaps Andrés has better insights.

Michael

Andres Taylor

unread,
May 8, 2012, 2:28:44 AM5/8/12
to ne...@googlegroups.com
On Mon, May 7, 2012 at 11:50 PM, Michael Hunger <michael...@neotechnology.com> wrote:
Marc,

the best I came up with is:
START gp=node(6) MATCH gp-[:B]->c<-[:B]-mp<-[:SW|G]-u with mp, collect(u) as us where NONE(n in us where n.name ='u1') return mp



This is my shot at it. There are plans to make this nicer - I'd like to skip the last part of the match (mp-[?]-p) and move that entirely to the WHERE, but not yet.

START gw1=node(6) 
MATCH gw1-[:B]->c<-[:B]-mp-[?:B]-p 
WHERE p is null or p.name!="u1" 
RETURN mp


HTH,

Andrés

Marc de Verdelhan

unread,
May 8, 2012, 3:28:20 AM5/8/12
to ne...@googlegroups.com
Andrés,

It seems you misunderstood what I'd like to do. I don't want to keep p1 but to remove it. Michael's solution works on this little graph. I'll test it on a bigger one tomorrow. However there should be a more elegant way to do this.

BTW you're right: moving the last part of the MATCH clause to the WHERE would be nicer.

Thank you guys.

Marc

Marc de Verdelhan

unread,
May 9, 2012, 4:12:47 AM5/9/12
to ne...@googlegroups.com
Just for saying I found simpler: start gp=node(6) match gp-[:B]->c<-[:B]-mp<-[:SW|G]-u with mp, collect(u.name) as uns where not('u1' in uns) return mp
Works well.

Cheers,

Marc

Peter Neubauer

unread,
May 9, 2012, 4:18:14 AM5/9/12
to ne...@googlegroups.com
Cool,
thanks for sharing Marc!

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


On Wed, May 9, 2012 at 10:12 AM, Marc de Verdelhan
Reply all
Reply to author
Forward
0 new messages