Cypher does not allow further processing of
UNION ALL results, since
RETURN is required in all queries of the union.
Here are some workarounds.
Post-UNION processing in Neo4j 4.0
With Neo4j 4.0, post-UNION processing is now possible via subqueries.
An example of usage:
This allows us to keep working with the results of the UNION subquery.
However, with the initial 4.0 release only uncorrelated subqueries are supported, meaning the subquery cannot use variables from outside the call. This means that using a subquery for post-UNION processing in the middle of a more complex query may not be possible, as you cannot pass in variables from the outer query for use within the subquery.
Correlated subqueries, which are more useful and can use variables from outside the subquery, are currently only available when using Neo4j Fabric, though this functionality is scheduled to be implemented for regular Cypher queries in a future minor release.
Combine collections, then
UNWIND back to rows and apply
DISTINCT isn’t really needed in the above query, but it will be needed if it’s possible for a result to be present in multiple collections being combined, provided you want distinct values.
Use apoc.cypher.run() to return
UNION results from a subquery
Using APOC Procedures, you can use
apoc.cypher.run() to execute a
UNION within a subquery, and return its results.
Remember that procedure calls are executed per-row, so using this approach when multiple rows already exist may lead to unintended and unexpected results.