Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

cypher - Neo4j - changing relationship type not working in web interface data browser

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...