I am assuming that you're looking for the combination of the nodes above that would be the union of properties based on a common name
property into one node with a distinct name
. Always an interesting question on how to combine things in a graph when a classic MERGE
statement does not quiet fit. Fortunately there's an apoc procedure apoc.refactor.mergeNodes that can help. The Cypher query below walks through the list of nodes by name and uses apoc.refactor.mergeNodes
to decide what to do with the properties. I changed the created_timestampe values to unique integers so that you can see that there's not an ordering. That doesn't seem to apply to this case, but is good to know. A test set of data is below the query and the results.
One other thought is to clean the data before as part of the load process which is usually easier to do.
// apoc.refactor.mergeNodes() to combine nodes by name and handle the node properties
// note: indexes are 0 based
MATCH (c:Contact)
WITH c.name AS name, COLLECT(c) AS contacts,
SIZE(COLLECT(c)) AS nbr_nodes
WHERE SIZE(contacts) > 1 // only want names that have more than one node
// uncomment the RETURN and delete lines below it to see the grouping
// RETURN name, contacts, SIZE(contacts)
WITH name as name, contacts as contacts, nbr_nodes AS nbr_nodes
UNWIND RANGE(1, nbr_nodes - 1) as idx
CALL apoc.refactor.mergeNodes([contacts[0], contacts[idx]], {properties: {name:'combine', created_timestampe:'overwrite', updated_timestamp:'overwrite', contact_point:'combine'}}) YIELD node
RETURN name, max(idx) + 1 AS `Nbr Nodes Merged`
OUTPUT
βββββββββ€βββββββββββββββββββ
β"name" β"Nbr Nodes Merged"β
βββββββββͺβββββββββββββββββββ‘
β"MamΓ " β3 β
βββββββββΌβββββββββββββββββββ€
β"Kiddo"β2 β
βββββββββΌβββββββββββββββββββ€
β"PapΓ " β4 β
βββββββββ΄βββββββββββββββββββ
MATCH(n) RETURN n
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β"n" β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ‘
β{"name":"MamΓ ","created_timestampe":5,"updated_timestamp":1520798382,"β
βcontact_point":"+39*********332"} β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β{"name":"Kiddo","created_timestampe":9,"updated_timestamp":1520798382,β
β"contact_point":["1234567","abcdefg"]} β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β{"name":"PapΓ ","created_timestampe":3,"updated_timestamp":1520798382,"β
βcontact_point":["plus-minus-plus-minus","+39********009","+39********0β
β0X","39*********332","+39*********332"]} β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β{"name":"The Dag","created_timestampe":10,"updated_timestamp":15207983β
β82,"contact_point":["food dish"]} β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Test Data
CREATE (:Contact {
name: "PapΓ ",
created_timestampe: 1,
updated_timestamp: 1493240714,
contact_point: [
"+39********009",
"+39********00X"
]
} ),
(:Contact {
name: "PapΓ ",
created_timestampe: 2,
updated_timestamp: 1493240714,
contact_point: [
"+39********009",
"39*********332"
]
}),
(:Contact {
name: "PapΓ ",
created_timestampe: 3,
updated_timestamp: 1520798382,
contact_point: [
"+39*********332"
]
}),
(:Contact {
name: "PapΓ ",
created_timestampe: 4,
updated_timestamp: 1520798382,
contact_point: [
"plus-minus-plus-minus"
]
}),
(:Contact {
name: "MamΓ ",
created_timestampe: 5,
updated_timestamp: 1520798382,
contact_point: [
"+39*********332"
]
}),
(:Contact {
name: "MamΓ ",
created_timestampe: 6,
updated_timestamp: 1520798382,
contact_point: [
"+39*********332"
]
}),
(:Contact {
name: "MamΓ ",
created_timestampe: 7,
updated_timestamp: 1520798382,
contact_point: [
"+39*********332"
]
}),
(:Contact {
name: "Kiddo",
created_timestampe: 8,
updated_timestamp: 1520798382,
contact_point: [
"1234567"
]
}),
(:Contact {
name: "Kiddo",
created_timestampe: 9,
updated_timestamp: 1520798382,
contact_point: [
"abcdefg"
]
}),
(:Contact {
name: "The Dag",
created_timestampe: 10,
updated_timestamp: 1520798382,
contact_point: [
"food dish"
]
})