I have a match that returns results 'con'. I then want to try to refine the results by optionally intersecting it with other patterns. If their is an intersection on a secondary pattern then those results should be returned otherwise the original should be returned intact.
My problem is that when there is no intersection the 'con' results become null so I can't return them as an alternative. This is a constant battle for me in a lot of my queries. What is a good method for intersecting a collection without loosing it when there is no match?
MATCH p0=(:node {name:”Sam”})-[:has*1]->(s0:friend)-[:sub*0..35]->(con)
MATCH p1=(:node {name:”Toby”})-[:has*1]->(s1:friend)-[:sub*0..35]->(con)
OPTIONAL MATCH (s0)-[:inst*1]-(a:ins)-[:inst*1]->(b:ins)<-[:inst*1]-(s1)
OPTIONAL MATCH (b)-[:inst|sub*0..40]->(c)
WITH apoc.coll.intersection(collect(distinct con),collect(distinct c)) as results,con
UNWIND results as co
RETURN DISTINCT
CASE WHEN co IS NULL THEN con ELSE co END AS res
I've returned 'con' separately as a test and where there is a 'co' there is still a 'con' but when 'co' is null so is 'con'.
Thanks for your help!