Create a set of relationships based on match output

Hi. I have a database containing U.S. Census data from the early 1900's. The relationships in the database are the ones given in the Census directly - each record is related to it's "Head of Household". I would like to infer additional relationships from this data.

For example, say you have a Head of Household named John. And John has 2 sons named Bill and Paul, and a daughter named Mary. I would like to infer the relationships between Bill, Paul and Mary and create that in the database (they are all siblings to each other). Assuming I use directional relations - this would create 6 connections in the database.

Every node in my database right now is a Person. Each person node has a GUID assigned to it.
This query :

match (f:Person)-[:IsParentOf]->(s:Person) return f.GUID, collect(s.GUID) as kids

will return all the Head of Household GUIDs along with a collection of their kids GUIDs.

My question is - how can I now pipe this output into a create clause that will create all 6 of my relations. I've tried playing around with Merge, Unwind, etc....frankly it's all quite confusing!

This is my first post here - I hope I've provided enough detail.


hey @hleiner1, this might be the solution to your problem:

WITH f.GUID AS father, collect(s.GUID) AS kids
UNWIND kids AS kid
WITH kid,[x IN kids WHERE x<>kid] AS list
UNWIND list as bro
MATCH (k:Person {GUID:kid}),(b:Person {GUID:bro})
MERGE (k)-[:IS_BROTHER]->(b)

@rushikesh Thank you so much! Unpacking that statement has taught me more in 15 minutes than I was able to glean from hours of reading the online documentation!

1 Like