I can't find how to return a node labels with Cypher.
Anybody knows the syntax for this operation?
To get all distinct node labels:
MATCH (n) RETURN distinct labels(n)
To get the node count for each label:
MATCH (n) RETURN distinct labels(n), count(*)
Neo4j 3.0 has introduced the procedure db.labels()
witch return all available labels in the database. Use:
call db.labels();
If you want all the individual labels (not the combinations) you can always expand on the answers:
MATCH (n)
WITH DISTINCT labels(n) AS labels
UNWIND labels AS label
RETURN DISTINCT label
ORDER BY label
START n=node(*) RETURN labels(n)
If you're using the Java API, you can quickly get an iterator of all the Label
s in the database like so:
GraphDatabaseService db = (new GraphDatabaseFactory()).newEmbeddedDatabase(pathToDatabase);
ResourceIterable<Label> labs = GlobalGraphOperations.at(db).getAllLabels();
If you want to get the labels of a specify node, then use labels(node)
; If you only want to get all node labels in neo4j, then use this function instead: call db.labels;
, never ever use this query: MATCH n RETURN DISTINCT LABELS(n)
. It will do a full table scan, which is very very slow..