Reputation: 395
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.
Upvotes: 4
Views: 1178
Reputation: 11216
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
Upvotes: 3
Reputation: 20185
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
Upvotes: 7
Reputation: 29172
//
// Get patterns
MATCH (a:Label1)-[r:REL]->(b:Label2)
//
// Pass through all keys
UNWIND KEYS(b) as key
//
// Get patterns again
MATCH (a:Label1)-[r:REL]->(b:Label2)
//
// Aggregating by title of the properties and its content
RETURN key as propName, b[key] as propValue, count(b[key]) as propRank
//
// Sort the largest property-value pairs first
ORDER BY propRank DESC
Upvotes: 2