简体   繁体   中英

Neo4J COALESCE in relationship

I am trying to run a relationship creation but for some reason I cant use coalesce inside of my relationship query. Im completely stuck and some of this may be malformed but im trying to fix it one step at a time. here is my code

/* Get the kill assassin and student by relation UUID*/
'MATCH (a:Student)-[r:TARGET]->(t:Student) WHERE r.uuid = $uuid ' +
/* Lets Kill the student and set confirmed to true */
'SET r.confirmed = true, t.IsDead = true WITH ' +
/* Delete any teachers that may have the target of the student*/
'MATCH (t)<-[tr:TARGET]-(:Teacher) DELETE r ' +
/* Get the assassinated persons target. Set them as X. */
'MATCH (t)-[ar:TARGET]->(x:Student) WHERE ar.confirmed = false AND x.IsDead = false ' +
/* Lets deal with anyone who inherited our target if we are dead :(*/
'OPTIONAL MATCH (t)<-[sr:TARGET]-(ss:Student) WHERE sr.confirmed = false AND ss.IsDead = false ' +
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
'CREATE (COALESCE(t,a))-[nr:TARGET {killed:false,confirmed:false}]->(t) '

EDIT: I cleaned up my code A LOT. Just for the record coalesce cant be used in a relationship. Here is the cleaned up code for those wondering.

/**
     * a: The student who made the kill
     * v: The victim of the kill
     * r: the relationship between a and t
     * x: the victims target
     * tr (OPTIONAL) : the teacher who we hired to kill our target
     * ss: The person who will be inheriting t(victim)'s target
     */
    /* Get the Killer, set them as A AND set the Victim as T */
    'MATCH (a:Student)-[r:TARGET {uuid:$uuid,confirmed:false}]->(v:Student),' +
    /* Fetch the Victims Target and set them as x */
    '(v)-[:TARGET {confirmed:false}]->(x:Student {IsDead:false}) ' +
    /* Check if we hired a teacher to assassinate our target. Set the relationship to TR (teacher relation)*/
    'OPTIONAL MATCH (v)<-[tr:TARGET]-(:Teacher) ' +
    /* Fetch the alive person who has the target (in-case we died and then it went into review) */
    'MATCH (v)<-[sr:TARGET {confirmed:false}]-(ss:Student {IsDead:false}) ' +
    /* Create a relationship between SS and t */
    'CREATE (ss)-[:TARGET {killed:false,confirmed:false}]->(x) ' +
    /* Set the Kill Relationship to complete and kill the victim */
    'SET r.confirmed = true, v.IsDead = true,a.Balance = a.Balance + 3 ' +
    /* Delete the teacher relationship */
    'DELETE tr ', {uuid:uuid}

You can't use an expression inside that CREATE - but you can move that expression into a WITH statement to assign it to a variable, and then use that variable in your create.

The last line could be replaced by:

/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
WITH COALESCE(t, a) as n, t
CREATE (n)-[nr:TARGET {killed:false,confirmed:false}]->(t)

Which compiles fine, but there are some other problems in the query I had to fix up first:

  • Line 2, SET r.comfirmed... ends in a WITH but there's no variables after it
  • Line 3, MATCH (t)<-... ends in a DELETE but the next line starts with a MATCH - you need a WITH in between the two

So the query I end up with is:

MATCH (a:Student)-[r:TARGET]->(t:Student) WHERE r.uuid = $uuid
/* Lets Kill the student and set confirmed to true */
SET r.confirmed = true, t.IsDead = true 
/* Delete any teachers that may have the target of the student*/
WITH a, t, r
MATCH (t)<-[tr:TARGET]-(:Teacher) DELETE r
/* Get the assassinated persons target. Set them as X. */
WITH a, t
MATCH (t)-[ar:TARGET]->(x:Student) WHERE ar.confirmed = false AND x.IsDead = false
/* Lets deal with anyone who inherited our target if we are dead :(*/
OPTIONAL MATCH (t)<-[sr:TARGET]-(ss:Student) WHERE sr.confirmed = false AND ss.IsDead = false
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
WITH COALESCE(t, a) as n, t
CREATE (n)-[nr:TARGET {killed:false,confirmed:false}]->(t)

But even then it looks wrong - the COALESCE will always resolve to t because to have gotten that far in the query t must have a value, so the last CREATE will just be creating a relationship between t and itself. Also: the OPTIONAL MATCH introduces the new variable ss but then the variable isn't used anywhere else (making the optional match somewhat redundant).

I'm guessing the COALESCE should be between ss and a , rather than t and a but it's hard to tell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM