cancel
Showing results for 
Search instead for 
Did you mean: 

How can I get shortest path after delete some relationships?

lingxuan9
Node Link

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

glilienfield
Ninja
Ninja

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

Cobra
Ninja
Ninja

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"

Cobra
Ninja
Ninja

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
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.