Conditional Where statement if parameter exists

I'm trying to write a query that takes four parameters which are all lists of ids and which should filter the results.

I have nodes called KnowledgeEntry which have multiple KnowledgeRecords attached to them
(:KnowledgeEntry)-[:CONTAINS]->(:KnowledgeRecord)

KnowledgeRecords each have four types of relations to nodes of type Location, Community, Function or OrganizationalUnit which all have an id attribute.

I want to retrieve all KnowledgeEntries if they have one or more KnowledgeRecords that have a relation to Location, Community, Function or OU where id matches one of the items in the given list (parameter)

This is what I came up with so far:

MATCH (knowledgeEntry:KnowledgeEntry)-[relationship]-(:KnowledgeEntry)
WHERE EXISTS {
    MATCH (knowledgeEntry)-[:CONTAINS]->(:KnowledgeRecord)-[:BASED_AT]->(loc:Location)
    WHERE loc.id IN $filters.locationIds
}

RETURN relationship, knowledgeEntry;
  1. This only works if all parameters used are set. If for example $filters.locationIds has no entries or is null the where statement should be ignored. I tried this with CASE statement and apoc.when but had no success.

  2. When adding an OR EXISTS for e.g OrganizationalUnit I get an error "There should be at least one pattern expression"

MATCH (knowledgeEntry:KnowledgeEntry)-[relationship]-(:KnowledgeEntry)
WHERE EXISTS {
    MATCH (knowledgeEntry)-[:CONTAINS]->(:KnowledgeRecord)-[:BASED_AT]->(loc:Location)
    WHERE loc.id IN $locationIds
}

OR EXISTS {
    MATCH (knowledgeEntry)-[:CONTAINS]->(:KnowledgeRecord)-[:RELEVANT_FOR_EMPLOYEES_WITH]->(ou:OrganizationalUnit)
    WHERE ou.id IN $organizationalUnitIds
}

RETURN relationship, knowledgeEntry;

Hi I know I'm grave digging, but I think I found a neat solution to this.

If you change the 'WHERE' line in your first code block to

WHERE ($locationIds IS NOT NULL AND loc.id IN $locationIds) OR ($locationIds IS NULL)

The part before 'OR' is similar to what you already had, but first checks $locationIds has a value.

The part after the 'OR' will be true if $locationIds doesn't have a value. Basically saying WHERE true, and that's the equivelent of having no where statement.

My suggestion above only works when $locationIds is either values or null, but it could be expanded for empty arrays also.