简体   繁体   中英

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?

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

通过这个密码查询,我们可以得到 neo4j 中存在的不同标签及其计数。

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

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.

You can leverage apoc library meta graph as described here https://stackoverflow.com/a/52489029 --> run the code below, it works even if there is more labels attached to a one node.

CALL apoc.meta.stats() YIELD labels
RETURN labels

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM