Merge of Relationships

(Intouch Vivek) #1

Hi,

Currently (Person) {first_name:Vivek} is joined with node Telephone {num:123456} on relationship TELEPHONE_NUM three times .
I need to combine the relationships TELEPHONE_NUM and make one relationship between them.

Following query

match (n1:Person) -[rel:TELEPHONE_NUM]-> (n2:Telephone) with collect(rel) as rels CALL apoc.refactor.mergeRelationships(rels,{properties:"combine"})
YIELD rel RETURN rel
Gave error: All Relationships must have the same start and end nodes.

Please let me know how to achieve this

-Vivek

0 Likes

(DKumar) #2

Hi Vivek,
Did you try create unique to create the relationship ?
It is depreciated, but give it a try.

0 Likes

(Intouch Vivek) #3

Thanks for your response. my dataset is like
|Vivek|Srivastava|9632196321|Datasource1|
|Vivek|Srivastava|9632196321|DataSource2|
|Vivek|Srivastava|9632196321|DataSource2|

property of Person are FirstName, LastName and Datasource
property of Telephone are number and Datasource
I need to have one relationship between Vivek and 9632196321 and relationship has property as number and array of datasource, in short i need to combine the properties of relationship

0 Likes

(DKumar) #4

you have 2 DataSource2. Is that the actual data ?

0 Likes

(Intouch Vivek) #5

yes .. this is my usecase
Data is coming from 2 different sources datasource1 and datasource2..

0 Likes

(Benjamin Squire) #6

3 Methods comes to mind:
1.)Either change how you import them, by matching first and then skipping if the rel exists, else make the rel.

2.) Following the import method of neo4j-admin import, break them into individual pieces and then use distinct pair wise... e.g. - persons.csv which is distinct first_name , telephones.csv which is distinct num and then pairwise distinct relationship person_to_tele.csv... first_name, num on each row distinctly.

3.) Match all nodes with have multiple rels between two nodes and delete the number of rels they have minus 1

0 Likes

(Intouch Vivek) #7

As per the requirement I need ingest data as it is ..and then do the manipulation.. For an example a user might be mapped to multiple SSN number or one SSN number is mapped to multiple users.. in this case i can't pre-process the data

0 Likes

(Benjamin Squire) #8

I looked at your query and when you collect(rel) it is just getting a list of ALL relationships between ALL person and Telephone nodes which is why you can't merge the relationships and it says they are different nodes.

try:

match (n1:Person) -[rel:TELEPHONE_NUM]-> (n2:Telephone) with n1,n2 collect(rel) as rels 
UNWIND rels  as  r CALL apoc.refactor.mergeRelationships(r,{properties:"combine"})
YIELD rel RETURN rel

something to do with UNWIND and using n1,n2 to collect.

if you look at

match (n1:Person) -[rel:TELEPHONE_NUM]-> (n2:Telephone) with n1,n2 size(collect(rel)) as relsize return n1,n1,relsize

compared to

match (n1:Person) -[rel:TELEPHONE_NUM]-> (n2:Telephone) with size(collect(rel)) as relsize return relsize

you can see you are just collecting all the relationships at once and not getting them grouped by n1,n2

0 Likes

(Intouch Vivek) #9

thanks a lot benjamin. Small change in the above code resolved the issue. We do not need to UNWIND rels as function apoc.refactor.mergeRelationships() works on a list

match (n1:Person) -[rel:TELEPHONE_NUM]-> (n2:Telephone) with n1,n2, collect(rel) as rels
CALL apoc.refactor.mergeRelationships(rels,{properties:"combine"})
YIELD rel RETURN rel

0 Likes