How can I get all property keys, for all nodes or for a given label, with Cypher/Neo4J?
Asked Answered
D

3

6

Neo4j has recently introduced call db.labels();, which yields all labels used in the database, presumably without the need to do a full scan.

Is there something similar for property keys, ie, some instruction to return all keys used in the DB, whatever the nodes? And is there something parameterized on the label, i.e., returning all the keys used in at least one node having a given label?

Again, I know how to do either query with full scans, but I'm afraid they won't be efficient. I know that such a function must be the one used by the web browser to show all property keys on a left column.

Droopy answered 26/2, 2018 at 16:39 Comment(0)
P
7

There is an APOC Procedure called apoc.meta.data. APOC documentation about this procedure says:

apoc.meta.data: examines a subset of the graph to provide a tabular meta information.

call apoc.meta.data();

produces:

╒═════════╤══════════╤═══════╤════════╤═══════╤═══════════╤═════════╤═══════╤════════╤═══════════╤════════════╤══════╤═══════╤═══════╤═════════════╤═════════════╕
│"label"  │"property"│"count"│"unique"│"index"│"existence"│"type"   │"array"│"sample"│"leftCount"│"rightCount"│"left"│"right"│"other"│"otherLabels"│"elementType"│
╞═════════╪══════════╪═══════╪════════╪═══════╪═══════════╪═════════╪═══════╪════════╪═══════════╪════════════╪══════╪═══════╪═══════╪═════════════╪═════════════╡
│"User"   │"age"     │0      │false   │false  │false      │"INTEGER"│false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
├─────────┼──────────┼───────┼────────┼───────┼───────────┼─────────┼───────┼────────┼───────────┼────────────┼──────┼───────┼───────┼─────────────┼─────────────┤
│"Product"│"name"    │0      │false   │false  │false      │"STRING" │false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
├─────────┼──────────┼───────┼────────┼───────┼───────────┼─────────┼───────┼────────┼───────────┼────────────┼──────┼───────┼───────┼─────────────┼─────────────┤
│"Product"│"price"   │0      │false   │false  │false      │"STRING" │false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
├─────────┼──────────┼───────┼────────┼───────┼───────────┼─────────┼───────┼────────┼───────────┼────────────┼──────┼───────┼───────┼─────────────┼─────────────┤
│"Product"│"color"   │0      │false   │false  │false      │"STRING" │false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
└─────────┴──────────┴───────┴────────┴───────┴───────────┴─────────┴───────┴────────┴───────────┴────────────┴──────┴───────┴───────┴─────────────┴─────────────┘

One way to filter is doing something like:

call apoc.meta.data() yield label, property
with ['Product', 'OtherLabel'] as labels, property, label where label in labels
return property, label

The above query returns results for Product and OtherLabel labels.

Pietje answered 26/2, 2018 at 16:55 Comment(0)
C
9

Try this below query, this will return all the labels in the graph along with the properties of nodes under each label:

MATCH(n) 
WITH LABELS(n) AS labels , KEYS(n) AS keys
UNWIND labels AS label
UNWIND keys AS key
RETURN DISTINCT label, COLLECT(DISTINCT key) AS props
ORDER BY label
Curtiscurtiss answered 11/9, 2019 at 17:38 Comment(1)
It takes 13350ms on my 1.5M node database. Which is fairly good, I was expecting much worse, so it could be an option where APOC isn't installed, thanks.Droopy
P
7

There is an APOC Procedure called apoc.meta.data. APOC documentation about this procedure says:

apoc.meta.data: examines a subset of the graph to provide a tabular meta information.

call apoc.meta.data();

produces:

╒═════════╤══════════╤═══════╤════════╤═══════╤═══════════╤═════════╤═══════╤════════╤═══════════╤════════════╤══════╤═══════╤═══════╤═════════════╤═════════════╕
│"label"  │"property"│"count"│"unique"│"index"│"existence"│"type"   │"array"│"sample"│"leftCount"│"rightCount"│"left"│"right"│"other"│"otherLabels"│"elementType"│
╞═════════╪══════════╪═══════╪════════╪═══════╪═══════════╪═════════╪═══════╪════════╪═══════════╪════════════╪══════╪═══════╪═══════╪═════════════╪═════════════╡
│"User"   │"age"     │0      │false   │false  │false      │"INTEGER"│false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
├─────────┼──────────┼───────┼────────┼───────┼───────────┼─────────┼───────┼────────┼───────────┼────────────┼──────┼───────┼───────┼─────────────┼─────────────┤
│"Product"│"name"    │0      │false   │false  │false      │"STRING" │false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
├─────────┼──────────┼───────┼────────┼───────┼───────────┼─────────┼───────┼────────┼───────────┼────────────┼──────┼───────┼───────┼─────────────┼─────────────┤
│"Product"│"price"   │0      │false   │false  │false      │"STRING" │false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
├─────────┼──────────┼───────┼────────┼───────┼───────────┼─────────┼───────┼────────┼───────────┼────────────┼──────┼───────┼───────┼─────────────┼─────────────┤
│"Product"│"color"   │0      │false   │false  │false      │"STRING" │false  │null    │0          │0           │0     │0      │[]     │[]           │"node"       │
└─────────┴──────────┴───────┴────────┴───────┴───────────┴─────────┴───────┴────────┴───────────┴────────────┴──────┴───────┴───────┴─────────────┴─────────────┘

One way to filter is doing something like:

call apoc.meta.data() yield label, property
with ['Product', 'OtherLabel'] as labels, property, label where label in labels
return property, label

The above query returns results for Product and OtherLabel labels.

Pietje answered 26/2, 2018 at 16:55 Comment(0)
G
3

Apart from what Trinadh had provided for non-apoc users on labels, this is the cypher call for relationship types:

MATCH (n)-[r]-(n1)
WITH type(r) AS type , KEYS(r) AS keys
UNWIND keys AS key
RETURN DISTINCT type, COLLECT(DISTINCT key) AS props
ORDER BY type
Grig answered 2/12, 2021 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.