简体   繁体   中英

neo4j how to return all node labels with Cypher?

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(*)

有一个函数标签(节点)可以返回节点的所有标签。

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

Neo4j 3.0 has introduced the procedure db.labels() witch return all available labels in the database. Use:

call db.labels();
 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..

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