Skip to content

Instantly share code, notes, and snippets.

@nickynicolson
Last active August 29, 2015 14:20
Show Gist options
  • Save nickynicolson/c6ff3fa8f173b35c27fc to your computer and use it in GitHub Desktop.
Save nickynicolson/c6ff3fa8f173b35c27fc to your computer and use it in GitHub Desktop.
Modification of the neo4j "meta" graph creation cypher statement to add counts to nodes and relationships
// Create meta-graph with orphan counts
MATCH (n)
/* ugly string replace to convert collection of labels to colon delimited string */
WITH replace(replace(replace(replace(str(labels(n)),'[',''),']',''),'"',''),',',':') AS l, count(distinct n) AS c
MERGE (mn:Meta_Node {name:l,count:c})
WITH mn
MATCH (a)-[r]->(b)
WITH replace(replace(replace(replace(str(labels(a)),'[',''),']',''),'"',''),',',':') AS a_labels
, count(distinct a) AS from_count
, type(r) AS rel_type
, count(distinct r) AS rcount
, replace(replace(replace(replace(str(labels(b)),'[',''),']',''),'"',''),',',':') AS b_labels
, count(distinct b) AS to_count
MATCH (a:Meta_Node {name:a_labels}), (b:Meta_Node {name:b_labels})
MERGE (a)-[mr:META_RELATIONSHIP {name:rel_type}]->(b)
ON CREATE SET mr.count = rcount, mr.from_orphans_count = (a.count - from_count), mr.to_orphans_count = (b.count - to_count)
RETURN distinct a as first_node, rel_type as connected_by, b as second_node
@nickynicolson
Copy link
Author

This also differs from the meta graph creation outlined here (http://blog.bruggen.com/2015/03/hidden-graphgems-revisited-22-meta-graph.html), as wholly abstract labels are not given their own meta nodes

@nickynicolson
Copy link
Author

Updated to add "orphan" counts to relationships, i.e. the number of "from" and "to" nodes which do not participate in said relationship.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment