1

I have some nodes (n1) and (n2) that have the same name but unique properties and share a node-pair relationship (s1)-[r]->(e1) as follows: before

I am trying to do a one-off where I create new relationships between s1 and e1 so that the new relationships are unique, based on id of n1 and n2, but the relationships have the same type as the previous relationship, and then delete the old relationship: after

I found the following: neo4j cypher: how to change the type of a relationship which guided me toward the following query:

MATCH (e1)<-[:ENDS_WITH]-(n {name:"same"})-[:STARTS_WITH]->(s1),
(s1)-[r]->(e1)
CREATE (s1)-[r2:NEWREL]->(e1)
// copy properties and set n_id unique
SET r2 = r, r2.n_id = n.n_id
WITH r
DELETE r

Makes perfect sense. However, when I attempt this in the Neo4j web interface data browser, the n_id property gets set properly, but the type(r2) gets set as string "NEWREL" rather than the expected "REL"

I tried back ticks around the NEWREL and passing a temporary variable from type(r). Not working.

Community
  • 1
  • 1
JeffA
  • 166
  • 1
  • 17

2 Answers2

1

[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.

Community
  • 1
  • 1
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • cybersam - Not sure what you mean. In the before scenario, there is only one relationship between s1 and e1. I want to create as many relationships as there are "parent" nodes, set the n_id property on each new relationship to be the same as the parent node n_id, and then delete the original relationship. – JeffA Mar 17 '16 at 21:21
  • cybersam - thanks. I am going to test that since I have a limited number of relationship types. I'll report back. – JeffA Mar 17 '16 at 22:32
  • cybersam - I keep getting an error **IllegalStateException** Any ideas why? – JeffA Mar 17 '16 at 23:09
  • yes - slightly different - instead of name I'm using label since all n nodes have the same label as follows: `MATCH (e1)<-[:ENDS_WITH]-(n:label1)-[:STARTS_WITH]->(s1)-[r]->(e1) WITH s1, e1, r, n, CASE TYPE(r) WHEN 'tests' THEN {rel1:[1]} WHEN 'decreases' THEN {rel2:[1]} WHEN 'increases' THEN {rel3:[1]} END AS todo FOREACH(x IN todo.rel1 | CREATE (s1)-[rr:tests]->(e1) SET rr = r, rr.n_id = n.n_id) FOREACH(x IN todo.rel2 | CREATE (s1)-[rr:decreases]->(e1) SET rr = r, rr.n_id = n.n_id) FOREACH(x IN todo.rel3 | CREATE (s1)-[rr:increases]->(e1) SET rr = r, rr.n_id = n.n_id) DELETE r' – JeffA Mar 17 '16 at 23:21
  • The query works for me using neo4j 3.0, but you are probably using 2.3, which seems to have a bug. If you do not have any properties on the `r` relationship worth copying, try changing all the `SET rr = r, rr.n_id = n.n_id` clauses to `SET rr.n_id = n.n_id`. – cybersam Mar 18 '16 at 00:16
  • Thanks. That worked when I used `SET rr.n_id = n.n_id` I appreciate the help. – JeffA Mar 18 '16 at 00:31
0

So the problem is that thee relationship type is NEWREL instead of REL? In line 3 you are specifying that type: CREATE (s1)-[r2:NEWREL]->(e1). Just change that to CREATE (s1)-[r2:REL]->(e1).

William Lyon
  • 8,371
  • 1
  • 17
  • 22
  • William - Thanks for responding. In this case, I don't know REL and the query was incorrect - now fixed by changing `(s1)-[r:REL]->(e1)` to `(s1)-[r]->(e1)` – JeffA Mar 17 '16 at 19:38