Cypher query to get all the nodes with more than one relations.

2,676 views
Skip to first unread message

lca...@gmail.com

unread,
Oct 4, 2013, 2:32:19 AM10/4/13
to ne...@googlegroups.com
Hi,
 I am evaluating neo4j for the first time. I was trying to find out all the nodes who have more than 1 relationship with other nodes. Basically I want to filter out all the nodes which have just one edge to any other node in the graph. How can I filter this using cypher Query? 

Regards,
LCassa.

Michael Hunger

unread,
Oct 4, 2013, 12:18:06 PM10/4/13
to ne...@googlegroups.com
match (n)--()
with n,count(*) as rel_cnt
where rel_cnt > 1
return n

Usually that query has to scan the whole graph, so it might not be fast for large graphs, you would want to restrict that to a subset of the nodes via labels or index lookups.

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

lca...@gmail.com

unread,
Oct 4, 2013, 4:07:13 PM10/4/13
to ne...@googlegroups.com
Hi Michael,
  I tried to run this query in shell. I get error.
  • match (n)--() with n,count(*) as rel_cnt where rel_cnt > 1 return n;
  • ==> Unknown command 'match'

..LCassa.

Michael Hunger

unread,
Oct 4, 2013, 4:16:31 PM10/4/13
to ne...@googlegroups.com
Oh that is 2.0 syntax, try

Start n=node(*)
match (n)--() with n,count(*) as rel_cnt where rel_cnt > 1 return n;

Sent from mobile device

lca...@gmail.com

unread,
Oct 4, 2013, 5:32:53 PM10/4/13
to ne...@googlegroups.com
Thanks that worked.

..LCassa

lca...@gmail.com

unread,
Oct 4, 2013, 5:41:08 PM10/4/13
to ne...@googlegroups.com
Hi,
 Sorry to bother again. I am trying to modify the query to get node and its relationships as well. But when I execute following query -
START x  = node(*)
MATCH x -[r]-> n
WITH x, count(*) as rel_cnt
WHERE rel_cnt > 1
RETURN type(r), n.name?, r;

I get , Unknown identifier errors. Cant find out, whats wrong with my query.

..LCassa

Koen Kleingeld

unread,
Oct 5, 2013, 2:10:24 AM10/5/13
to ne...@googlegroups.com
you have to add   r   to the with 


2013/10/4 <lca...@gmail.com>



--
Groeten / Best Regards,

Koen

[:Koen]

unread,
Oct 5, 2013, 2:13:11 AM10/5/13
to ne...@googlegroups.com
and    you also have to add     n to the with statement

Op vrijdag 4 oktober 2013 08:32:19 UTC+2 schreef lca...@gmail.com:
Reply all
Reply to author
Forward
0 new messages