bconneen
bconneen

Reputation: 154

Cypher Query for all nodes that have shared relationships to the 2 or more sub-nodes?

Created a GraphGist to help explain my issue:

https://gist.github.com/bconneen/e5c66e26883958c81ae6fc5c607fdfa9

Consider the following Neo4J model:

Applicant(firstName, lastName, uniqueId)
Phone(number)
IpAddress(ip)
BankAccount(routing,account)

Applicant--has->Phone,
Applicant--has->IpAddress,
Applicant--has-->BankAccount

All nodes are created using merge. So if we process 100,000 applications relationships will be created where occasionally more than 1 Applicant shares one or more of Phone, IpAddress, BankAccount.

I want the nodes returned to be when 2 or more Applicants share 2 or more Phone, IpAddress or BankAccount. I want to write a query that returns All Applicants and their shared relationships. That meet the criteria.

Example:

Applicant(John Smith) has Phone (555-555-5555), has ipAddress (127.0.0.1), has BankAccount (ABCDEF)

Applicant(Jane Doe) has Phone(222-222-2222, has ipAddress (127.0.0.1), has BankAccount (ABCDEF)

Applicant(Steve Zahn) has Phone(555-555-5555), has ipAddress(127.2.2.2), has BankAccount(GHJKD)

Applicant(James Clay) has Phone(444-444-4444), has ipAddress(129.3.3.3), has BankAccount(ZYXWVU)

Query for All Applicants that share 2 or more Phone, IpAddress or Bank:

Applicant(John Smith) has Phone (555-555-5555), has ipAddress (127.0.0.1), has BankAccount (ABCDEF)

Applicant(Jane Doe) has Phone(222-222-2222, has ipAddress (127.0.0.1), has BankAccount (ABCDEF)

Query for All Applicants that share 1 or more Phone, IpAddress or Bank:

Applicant(John Smith) has Phone (555-555-5555), has ipAddress (127.0.0.1), has BankAccount (ABCDEF)

Applicant(Jane Doe) has Phone(222-222-2222, has ipAddress (127.0.0.1), has BankAccount (ABCDEF)

Applicant(Steve Zahn) has Phone(555-555-5555), has ipAddress(127.2.2.2), has BankAccount(GHJKD)

Upvotes: 0

Views: 546

Answers (1)

William Lyon
William Lyon

Reputation: 8546

Using the data from your gist:

Query for all applicants that share 2 or more phone, ipaddress or bank

MATCH (applicant:Applicant)-[r]->(subelement)<-[r2]-(other:Applicant)
WITH applicant, other, collect(subelement) AS overlap 
WHERE id(applicant) > id(other) AND size(overlap) > 1
RETURN applicant, other, overlap

enter image description here

Upvotes: 2

Related Questions