Finding one path OR another one between nodes

I'm trying the find all the endpoints from several different paths from a starting node

i..e (a:foo)-[:rel1|rel2]->(b:bar). OR (a:foo)-[:rel3|:rel4]->(c:fizz)

So far I've come up with this (which works)

OPTIONAL MATCH (a:foo)-[r1:rel1|rel2]->(b:bar) 

OPTIONAL MATCH (a)-[r2:rel3|rel4]->(c:fizz) 

WHERE b is not null or c is not null 

RETURN DISTINCT a.name, r1.id, b.name, r2.id, c.name");

Is there a better way to do this?

OPTIONAL MATCH (a:foo)-[r1:rel1|rel2]->(b:bar) 
UNWIND r1 as rels
RETURN startNode(rels) as startNode, type(rels) as type, endNode(rels) as endNode
Results in two rows each with one relationship type.

I am not sure the intent of the query, as you don't use the 'b' node in your output. Are you just interested in the relationships ids, and not the end nodes?

I generalized the output a bit, so you can see a different approach. I used a UNION clause to execute the two queries and return one result set with the same set of columns. I included a constant 'relTye' so you can differentiate the rows from the first query from the second, if that is important to you. It is just a concept that you can adapt to meet your needs.

MATCH (a:foo)
CALL (a) {
    MATCH (a)-[r:rel1|rel2]->(b:bar)
    RETURN a as startNode, r as rel, b as endNode, 'relType1' as relType

    UNION 

    MATCH (a)-[r:rel3|r3]->(c:fizz) 
    RETURN a as startNode, r as rel, c as endNode, 'relType2' as relType
}

RETURN startNode.name, rel.id, endNode.name, relType

Question: Is the 'r3' in the relationship type condition of the second query correct?

1 Like

Oh that was a mistake in my transcription. Yes I mean to return b and c. I changed r3 to rel4 to make it clear it was deliberate.

Then what I provided should work.

1 Like