Cypher does not allow further processing of UNION or 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:

CALL {
  MATCH (movieOrPerson:Movie) RETURN movieOrPerson
  UNION
  MATCH (movieOrPerson:Person) RETURN movieOrPerson
}
WITH movieOrPerson
...

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

For 3.5.x and earlier
MATCH (m:Movie)
WITH collect(m) AS movies
MATCH (p:Person)
WITH movies + collect(p) AS moviesAndPeople
UNWIND moviesAndPeople AS movieOrPerson
WITH DISTINCT movieOrPerson
...

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.

CALL apoc.cypher.run('
 MATCH (movieOrPerson:Movie)
 RETURN movieOrPerson
 UNION
 MATCH (movieOrPerson:Person)
 RETURN movieOrPerson',
 {}) yield value
WITH value.movieOrPerson as movieOrPerson
...

Remember that procedure calls are executed per-row, so using this approach when multiple rows already exist may lead to unintended and unexpected results.

Details


Author:
Andrew Bowman
Applicable versions:
4.0, 3.5, 3.4, 3.3, 3.2, 3.1, 3.0
Keywords:
cypherneo4j-3.0neo4j-3.1neo4j-3.2neo4j-3.3neo4j-3.4neo4j-3.5neo4j-4.0UNION