Is there a way I can use cypher to derive the most common (and second most common) property value of a particular node type?
At the moment I am identifying the different values for the property:
MATCH(a:Label1)-[r:REL]->(b:Label2) RETURN DISTINCT b.prop;
And then counting them individually:
MATCH(a:Label1)-[r:REL]->(b:Label2) WHERE b.prop = "x" RETURN COUNT(x);
Any help would be appreciated, I am using Neo4j 3.0.0.
I'm not sure how to combine these queries.
It is as easy as :
MATCH (a:Label1)-[r:REL]->(b:Label2)
RETURN b.prop, count(*) as occurences
ORDER BY occurences DESC
LIMIT 2
You can read about automatic aggregation here : http://neo4j.com/docs/stable/query-aggregation.html
You can use the keys()
function to get the properties from each matching node. This returns a collection of keys for that node. Unwind the collections and aggregate the counts for each property accross all matching nodes.
MATCH (a:Label1)-[r:REL]->(b:Label2)
UNWIND keys(b) as prop
RETURN prop, count(*) as num_props
ORDER BY num_props desc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With