How to get different labels and their count in neo4j by cypher query?

I was needing to check what different labels present in graph database neo4j.

How to get different labels and their count in neo4j by cypher query?

Answers


It's surprisingly complex to get a count per label since nodes can have multiple labels and labels (n) returns a collection of strings representing those labels. On a graph consisting of three nodes and two labels, as {:A}, {:B} and {:A:B}, labels (n) returns three distinct string collections. Instead of counting two nodes with :A and two nodes with :B, the result would be one for each of the three label combinations. See console. To aggregate per label, not per label collection, you'd have to group by the values within collection, which is cumbersome.

I have an ugly way to do it, maybe someone can suggest a better one: first find out max number of labels any node has.

MATCH (n)
RETURN max(length(labels(n)))

Then chain that many queries with UNION, counting nodes by the label at position i in the collection, where i starts at 0 and increments to the max-1. If the nodes have at most 3 labels,

MATCH (n)
RETURN labels (n)[0] as name, count (n) as cnt
UNION MATCH (n)
RETURN labels (n)[1] as name, count (n) as cnt
UNION MATCH (n)
RETURN labels (n)[2] as name, count (n) as cnt

This aggregates the label counts correctly, but it returns a null count for every case where the index is out of the collection bounds. For the first return (the [0] index) this signifies nodes that don't have a label. For the other lines, the null count similarly signifies nodes with less labels than queried for, but this information is irrelevant, so can be ignored

MATCH (n)
RETURN labels (n)[0] as name, count (n) as cnt
UNION MATCH (n)
WITH labels (n)[1] as name, count (n) as cnt
WHERE name IS NOT NULL
RETURN name, cnt
UNION MATCH (n)
WITH labels (n)[2] as name, count (n) as cnt
WHERE name IS NOT NULL
RETURN name, cnt

I'm sure this could be done more gracefully, but that's as far as I got.


I finally found a solution to the multiple label problem that is less complicated:

MATCH (a) WITH DISTINCT LABELS(a) AS temp, COUNT(a) AS tempCnt
UNWIND temp AS label
RETURN label, SUM(tempCnt) AS cnt

With this cypher query we can get different labels present in neo4j and their count.

MATCH (n) RETURN DISTINCT LABELS(n), COUNT(n)

Need Your Help

how to define package structure for a Spring REST MVC application?

spring rest spring-mvc project-structure

I am new to writing a spring rest based ws. I created a project with the following structure.