Search code examples
cypherredisgraph

Changing node reference in RedisGraph relationship


I'm starting to use RedisGraph and got some good support here and glad there's a community around it :)

I'm currently trying to achieve a basic thing: moving a relation to a different node:

(bob:User {uid: 1})-[p:Paid {amount: 5, date: "Feb 5 2021 10:10:05"}]->(alice:User {uid: 2})

Bob was mistaken, he didn't pay Alice but John and would like to fix it.

(bob)-[p]->(john:User {uid: 3})

I must preserve the properties for [p] and ideally I would even keep the same id…

Neo4j has the apoc.refactor.mergeNodes function does that but I'm not sure how to achieve a similar result in RedisGraph.

There seems to be a way with SET, WITH and DELETE but I couldn't get something grammatically correct that would work.

Any clue about that?

Thanks for sharing! :D


Solution

  • RedisGraph doesn't have an equivalent to the mergeNodes functionality, nor does it support changing the endpoint of an existing edge.

    As such, you will need to delete the original edge and introduce a new one:

    MATCH (bob:User {uid: 1})-[p:Paid {amount: 5, date: "Feb 5 2021 10:10:05"}]->(alice:User {uid: 2}) WITH bob, p, {amount: p.amount, date: p.date} AS props MATCH (john:User {uid: 3}) DELETE p CREATE (bob)-[p2:Paid]->(john) SET p2=props
    

    This can be simplified a bit if your program is aware of the property values and can use them as literals:

    MATCH (bob:User {uid: 1})-[p:Paid {amount: 5, date: "Feb 5 2021 10:10:05"}]->(alice:User {uid: 2}) DELETE p CREATE (bob)-[p2:Paid {amount: 5, date: "Feb 5 2021 10:10:05"}]->(john)
    

    Either approach will cause the new edge to have the same ID as the previous one, as the edge ID is released by the DELETE and immediately reused by the CREATE.