Skip to content

Instantly share code, notes, and snippets.

@bconneen
Forked from jexp/graph_gist_template.adoc
Last active March 31, 2016 03:05
Show Gist options
  • Save bconneen/e5c66e26883958c81ae6fc5c607fdfa9 to your computer and use it in GitHub Desktop.
Save bconneen/e5c66e26883958c81ae6fc5c607fdfa9 to your computer and use it in GitHub Desktop.
CHANGEME: GraphGist Template. Fork to make your own, view source to see instruction comments

COMMONALITY: FIND NODES WITH 2 OR MORE NODES IN COMMON

Introduction

This is an example for finding nodes that share multiple points of commonality.

skitch

Setup

// First Applicant
MERGE (applicant_a:Applicant{firstName:'JOSE', lastName:'JONES'})
MERGE (phone1_a:Phone{number:'5555555555'})
MERGE (phone1_a)<-[:HAS_PHONE]-(applicant_a)
MERGE (phone2_a:Phone{number:'2222222222'})
MERGE (phone2_a)<-[:HAS_PHONE]-(applicant_a)
MERGE (ipAddress_a:IpAddress{ip:'204.192.128.33'})
MERGE (ipAddress_a)<-[:HAS_IP]-(applicant_a)
MERGE (bankAccount_a:BankAccount{routing:'12345678',account:'456789'})
MERGE (bankAccount_a)<-[:HAS_ACCOUNT]-(applicant_a)

// Second Applicant
MERGE (applicant_b:Applicant{firstName:'BRIAN', lastName:'CRAST'})
MERGE (phone1_b:Phone{number:'4444444444'})
MERGE (phone1_b)<-[:HAS_PHONE]-(applicant_b)
MERGE (phone2_b:Phone{number:'9999999999'})
MERGE (phone2_b)<-[:HAS_PHONE]-(applicant_b)
MERGE (ipAddress_b:IpAddress{ip:'127.0.0.1'})
MERGE (ipAddress_b)<-[:HAS_IP]-(applicant_b)
MERGE (bankAccount_b:BankAccount{routing:'9876543',account:'123456'})
MERGE (bankAccount_b)<-[:HAS_ACCOUNT]-(applicant_b)

// Third Applicant
MERGE (applicant_c:Applicant{firstName:'STEVE', lastName:'DUNPHY'})
MERGE (phone1_c:Phone{number:'2222222222'})
MERGE (phone1_c)<-[:HAS_PHONE]-(applicant_c)
MERGE (phone2_c:Phone{number:'6666666666'})
MERGE (phone2_c)<-[:HAS_PHONE]-(applicant_c)
MERGE (ipAddress_c:IpAddress{ip:'204.192.128.33'})
MERGE (ipAddress_c)<-[:HAS_IP]-(applicant_c)
MERGE (bankAccount_c:BankAccount{routing:'3333333',account:'123454'})
MERGE (bankAccount_c)<-[:HAS_ACCOUNT]-(applicant_c)

// Fourth Applicant
MERGE (applicant_d:Applicant{firstName:'NIKKI', lastName:'COX'})
MERGE (phone1_d:Phone{number:'3333333333'})
MERGE (phone1_d)<-[:HAS_PHONE]-(applicant_d)
MERGE (phone2_d:Phone{number:'7777777777'})
MERGE (phone2_d)<-[:HAS_PHONE]-(applicant_d)
MERGE (ipAddress_d:IpAddress{ip:'202.185.123.11'})
MERGE (ipAddress_d)<-[:HAS_IP]-(applicant_d)
MERGE (bankAccount_d:BankAccount{routing:'12345678',account:'456789'})
MERGE (bankAccount_d)<-[:HAS_ACCOUNT]-(applicant_d)

Find Nodes with 1 or more Relationships in Common

Trying to find all Applicant nodes that have 1 or more of Phone, IpAddress or BankAccount in common

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

Find Nodes with 2 or more Relationships in Common

Trying to find all Applicant nodes that have 2 or more of Phone, IpAddress or BankAccount in common

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

Created by BRIAN CONNEEN - Twitter | LinkedIn

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