Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-02-2019 05:37 AM
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
04-02-2019 05:59 AM
Hi Vivek,
Did you try create unique to create the relationship ?
It is depreciated, but give it a try.
04-02-2019 06:58 AM
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
04-02-2019 08:10 AM
you have 2 DataSource2. Is that the actual data ?
04-02-2019 08:11 AM
yes .. this is my usecase
Data is coming from 2 different sources datasource1 and datasource2..
04-02-2019 09:49 AM
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
04-02-2019 10:55 AM
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
04-02-2019 03:56 PM
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
04-03-2019 01:35 AM
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
All the sessions of the conference are now available online