I want to retrieve a node by a potentially existing source ID, cause this is very fast. In case, there is no source ID (yet), I would then run a different, slower query.
I want to do all that in one statement.
I have something like this in mind:
MATCH (n:Node)-[rel:HAS_SOURCE_ID {id: "1234"})->(src:Source {name:"providerA"})
WITH n, COUNT(n) AS numN WHERE numN = 0
MATCH (n:Node)-[rel:SOME_OTHER_RELATION]->(otherNode:OtherNode) WHERE otherNode.name = "something" AND rel.property < 12
RETURN n
The individual MATCH statements do work, but the combined one does not return any values, independent of the number of results of the first MATCH.
OPTIONAL MATCH (n:Node)-[rel:HAS_SOURCE_ID {id: "1234"}]->(src:Source {name:"providerA"})
Call {
WITH n
WITH n
WHERE not n is null
RETURN n as result
UNION
WITH n
WITH n
WHERE n is null
MATCH (result:Node)-[rel:SOME_OTHER_RELATION]->(otherNode:OtherNode)
WHERE otherNode.name = "something" AND rel.property < 12
RETURN result
}
RETURN result
That works! Thanks again, @glilienfield!
This double WITH n seems a bit un-intuitive, but it seems as if the first WITH n refers to the variable outside of CALL and the WITH n WHERE NOT n IS NULL then filters this result set...
That is correct. The documentation states the “with” to import variables in a “call” subquery must be “simple”, I.e. it can’t have expressions, nor “where”, “limit”, skip”, or “order by” clauses normally available with a “with”.
The work around is to have a second “with” clause. The second one is not limit, so the “where” can be used. You are correct, it is not intuitive. I think it also looks silly. It would be nice if this is changed one day.