Question

I have a graph in which each connected component has a certain label let's say comp1, comp2, etc. I want to make a cypher query that returns all the labels that have more than one node. I get all the labels like this:

match (n) return labels(n)

So i tried to do something like this in order to get only the labels that I needed:

match (n) with labels(n) as lb where count(k:lb[0]) >= 2)  return lb limit 10

but I get a syntax error:

Invalid input ')': expected Digits, '.', 'E', whitespace, node labels, '[', "=~", IN, IS, '*', '/', '%', '^', '+', '-', '<', '>', "<=", ">=", '=', "<>", "!=", AND, XOR, OR, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, RETURN, UNION, ';' or end of input (line 1, column 57)

I would also want to order the labels by the number of nodes that have that label...

Was it helpful?

Solution

With 2.1

match (n) 
unwind labels(n) as l
with l,count(*) as cnt
where cnt > 2
return l

OTHER TIPS

if you're doing this with Neo4j version 2.0 you can achieve what you want with this cypher query:

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

but be aware that this query will transverse the whole graph so, it probably is a good idea to restrict it to certain labels. Cheers.

UPDATE

I read the question as nodes with more than 1 relation, my bad. This query wont do what the OP asked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top