Given 3 expressions, cypher to pick unique node matching

I have lot of nodes with properties/attributes: p1, p2, p3, p4, p5 etc;

Give an array of 3 regular expressions:

exp1 -> p1 = 1
exp2 -> p2 = 2 OR p3 = 3
exp3 -> p4 = 4 AND p5 = 5

Each expression match could result with 0, 1, or more nodes.

Can you help in writing single cypher query to pick the unique node, if any of the above expressions could matching with exactly one node?

Return null, if all given expressions match with more than 1 node OR no nodes.


What I tried?
Right now, I am using this query and wrote application code to check if any of the collection contains unique/only one node:
Optional MATCH (n1) WHERE n1.p1=1 WITH COLLECT(n1) as c1
Optional MATCH (n2) WHERE (n2.p2=2 OR n2.p3=3 ) WITH c1,COLLECT(n2) as c2
Optional MATCH (n3) WHERE (n3.p4=4 AND n3.p5=5 ) WITH c0,c1,COLLECT(n2) as c3
RETURN c1,c2,c3

What I want?
Rather than returning collections, If any of collection contains only 1 element, return it or return null.

I am not sure I fully understand the requirements, but maybe this will help:

WITH 
    COLLECT {MATCH (n) WHERE n.p1=1 RETURN n} as c1,
    COLLECT {MATCH (n) WHERE n.p2=2 OR n.p3=3 RETURN n} as c2,
    COLLECT {MATCH (n) WHERE n.p4=4 AND n.p5=5 RETURN n} as c3
RETURN 
    CASE WHEN size(c1) = 1 THEN head(c1) ELSE null END as node1,
    CASE WHEN size(c2) = 1 THEN head(c2) ELSE null END as node2,
    CASE WHEN size(c3) = 1 THEN head(c3) ELSE null END as node3

Hi Glilienfield, thank you for the clean query, this will help!

Glad to help. One comment, you do not have a label constraint on your matches. This will cause a full database scan of every node. Three times in fact. Can you add a label constraint? At least that will limit the scan to just that label. You can then add an index for each of the properties to improve the match performance.