Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to return a distinct list of all property keys for a specific labeled node

I need to get a distinct listing of all the properties that have been created on a set of nodes in our graph. Everything I know to do or finding in searches is going to return a listing of all nodes of such label and their individual distinct properties. For example:

CREATE (p:Person { name: 'John', age: 40, address: '100 main st' });
CREATE (a:Animal { name: 'Spot', type: 'dog', age: 2, diet: 'dry food'});
CREATE (a:Animal { name: 'Goldie', type: 'fish', family: 'carp' });

I want to get a distinct list back that just returns all the property keys for all Animals.

[ name, type, age, diet, family ]

doing something like this:

MATCH (a:Animal) RETURN distinct keys(a);

returns a list for each node:

[name, type, age, diet]
[name, type, family]

I just want a list of existing property keys for all of one label type. It also needs to be able to scale to millions of nodes.

like image 776
Ben A Avatar asked Sep 16 '25 02:09

Ben A


1 Answers

Just have to unwind the key collections and recombine them.

MATCH (a:Animal) 
UNWIND keys(a) AS key
RETURN collect(distinct key)

Alternatively, if you wanted them sorted alphabetically you could do something like this

MATCH (a:Animal) 
UNWIND keys(a) AS key
WITH DISTINCT key
ORDER by key
RETURN collect(key)

Or you could return the count of each key

MATCH (a:Animal) 
UNWIND keys(a) AS key
RETURN key, count(*) AS num
ORDER by num DESC, key
like image 185
Dave Bennett Avatar answered Sep 17 '25 18:09

Dave Bennett