As @florent_biville suggested, use OPTIONAL MATCH to return 'null' when a match is not found, instead of terminating the query. Here is one approach:'
OPTIONAL MATCH (m:label2)
WHERE m.name="my_name"
OPTIONAL MATCH (n:label1)<-[rel1]-(m)-[rel2]->(o:label3)-->(p:label4)
RETURN n.name, m.name, o.name, p.name, rel1.name, rel2.name
Can you explain a little more about what you are trying to accomplish?
When matching you are specifying the exact pattern you are looking for. If it can not be found, the search stops and a result it not returned.
In your initial query you ask for a path where the given node ‘m’ is related to a label1 node and a label3 node. You will get zero paths if ‘m’ is only related to one other node, one path if related to one each, and multiple paths if related to more than one each of label1 and label3 nodes. .
MATCH (n:label1)<-[rel1]-(m:label2)-[rel2]->(o:label3)
WHERE m.name="my_name"
RETURN n.name, m.name, o.name, p.name, rel1.name, rel2.name
If you add another constraint, like ‘o’ itself being related to a label4 node, then again, no result if the new relation does not exists.
In this case, you may do the following if the label4 node is optional.
MATCH (n:label1)<-[rel1]-(m:label2)-[rel2]->(o:label3)
WHERE m.name="my_name"
OPTIONAL MATCH (o)—>(p:label4)
RETURN n.name, m.name, o.name, p.name, rel1.name, rel2.name, p.name