[UPDATED]
Cypher only allows you to create a relationship using a hardcoded type, so in general there is no way for you to achieve what you want.
However, if you know beforehand all the possible relationship types, there is a workaround. In the following example, suppose the possible relationship types are REL1
, REL2
, REL3
, and REL4
:
MATCH (e1)<-[:ENDS_WITH]-(n {name:"same"})-[:STARTS_WITH]->(s1)-[r]->(e1)
WITH s1, e1, r, n,
CASE TYPE(r)
WHEN 'REL1' THEN {rel1:[1]}
WHEN 'REL2' THEN {rel2:[1]}
WHEN 'REL3' THEN {rel3:[1]}
WHEN 'REL4' THEN {rel4:[1]}
END AS todo
FOREACH(x IN todo.rel1 | CREATE (s1)-[rr:REL1]->(e1) SET rr = r, rr.n_id = n.n_id)
FOREACH(x IN todo.rel2 | CREATE (s1)-[rr:REL2]->(e1) SET rr = r, rr.n_id = n.n_id)
FOREACH(x IN todo.rel3 | CREATE (s1)-[rr:REL3]->(e1) SET rr = r, rr.n_id = n.n_id)
FOREACH(x IN todo.rel4 | CREATE (s1)-[rr:REL4]->(e1) SET rr = r, rr.n_id = n.n_id)
DELETE r
Only one of the FOREACH
clauses will actually create a new relationship (of the correct type) and copy the existing properties.
Caveat
The above solution works in neo4j 3.0, but neo4j 2.3 seems to have a bug that makes the query fail. If you do not have any properties on the r
relationship worth copying, changing all the SET rr = r, rr.n_id = n.n_id
clauses to SET rr.n_id = n.n_id
should avoid that issue on 2.3.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…