How to get different tags and their score in neo4j for cypher?

I needed to check what different labels are present in the neo4j chart database.

How to get different tags and their score in neo4j for cypher?

+4
source share
3 answers

It is surprisingly difficult to get counts per label, because nodes can have multiple labels, and labels (n)returns a collection of strings representing these labels. On a graph consisting of three nodes and two labels, as {:A}, {:B}and {:A:B}, labels (n)returns three different sets of rows. Instead of counting two nodes with :Aand two nodes with :B, the result will be one for each of the three combinations of labels. See console . To aggregate each label, not a collection of labels, you will have to group the values ​​inside the collection, which is cumbersome.

I have an ugly way to do this, maybe someone can suggest a better option: first find the maximum number of labels that any node has.

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

UNION, i , i 0 max-1. 3 ,

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

, null , . ( [0]) , . , , ,

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

, , , .

+4

, , :

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

cypher- , neo4j, .

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

Source: https://habr.com/ru/post/1538352/


All Articles