Skip to content

Instantly share code, notes, and snippets.

@rdkls
Last active March 8, 2018 00:59
Show Gist options
  • Save rdkls/3766b2f43f62f04ccaeae3d7e824e330 to your computer and use it in GitHub Desktop.
Save rdkls/3766b2f43f62f04ccaeae3d7e824e330 to your computer and use it in GitHub Desktop.
neo4j create schema based on existing nodes
// Generate an overview / schema of a graph database
// Creates a node for each node label found in the graph,
// relating these to each other for each type of relationship found in the graph
// (apoc call required to create rel with dynamic type)
MATCH (a)-[r]->(b)
WITH head(labels(a)) AS l, head(labels(b)) AS l2, type(r) AS rel_type, count(*) as num_links
merge (aMeta:Meta {label: l})
merge (bMeta:Meta {label: l2})
with aMeta, bMeta, rel_type, num_links
call apoc.create.relationship(aMeta, rel_type, {num_links: num_links}, bMeta) yield rel
with aMeta, bMeta
return aMeta, bMeta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment