Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Conditional parts of query

HI, I'm a bit stuck building a query.

I need to find all nodes that are connected to certain other nodes.

this is the query I have currently build:

match (rec:Mutation) where rec.cntn_pk in [ 88, 6256, 28551, 28894, 32298, 78073, 96890, 108773, 48338 ] with collect(rec) as records 
MATCH (s:Strain) 
WHERE ALL(r in records WHERE (s)-[:has_mutation]-(r)) 
with s 
match (rec2:Plasmid) where rec2.cntn_pk in [88, 6256, 28551, 28894, 32298, 78073, 96890, 108773, 48338] with collect(rec2) as records2 
MATCH (s:Strain) 
WHERE ALL(r2 in records2 WHERE (s)-[:has_construct]-(r2)) 
return s.cntn_pk, s.cntn_cf_id

the list is the input list and are basically id of nodes of different types. Depending on the type, I know how far off a node is allowed to be to find a hit.

So basically I try to fetch the nodes that correspond to the type Mutation and then find all strains that are connected to all these nodes over :has_mutation.
The query continues and then goes over plasmids over :has_construct.

Everything works, however, when the internmediate match does not find anything (e.g. records or records2 are empty lists, it basically return a lot (as there are quiet a couple of nodes that don't have any connection. In the case it find nothing, it should skip the first following query of strain.

How do I fix this? I thought to make a part of the query optional based on size(records)/count(records), but this seems really difficult.
Can this be done?