cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How can I get shortest path after delete some relationships?

I would like to delete some relationships in a graph first, and then find all pair shortest path.
Step1: MATCH (n {happy: false})-[r]-() DELETE r
Step2: MATCH p=allShortestPaths((h)-[*1..20]->(k {value:true}))

How can I combine these two steps in one query?

4 REPLIES 4

I don't think you can. It looks like DELETE is a terminal operation. I tried the query below to test. Although it executed without an error and deleted the relationship, it did not return the nodes n and m, even though a result is obtained if the second match/return is executed by itself. I suspect nothing is returned from the delete, so the query stops after the delete.

match(n{key:3})-[r:CONNECTED_TO]->(m{key:4}) delete r
with n
match(n{key:1})-[:CONNECTED_TO]->(m{key:2})
return n, m

Hello @lingxuan9

There is a workaround using the UNION keyword but this is an abuse and there is no guarantee that it will work in the future:

MATCH (n {happy: false})-[r]-() DELETE r
UNION
MATCH p=allShortestPaths((h)-[*1..20]->(k {value:true}))

The best option is to stick to two queries.

Regards,
Cobra

Thanks Cobra, I try it. But it shows
"All sub queries in an UNION must have the same column names"

So it was patched It was working on previous versions.

Can you try:

MATCH (n {happy: false})-[r]-() DELETE r
WITH *
MATCH p = allShortestPaths((h)-[*1..20]->(k {value:true}))
RETURN p