cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Optional call

Assume that I want to UNION the results of two cypher queries and ORDER BY them by some property. One solution is to use a method similar to the one mentioned in here. In the method if we replace the MATCHes with OPTIONAL MATCH we probably can handle the case that one of the two sub-queries has an empty result. However, if in our application we need to use CALL instead of MATCH, I could not find a way for handling that case. For example, assume that we have two queries like "CALL db.index.fulltext.queryNodes(...)" and we want to UNION the results. How should we do that? Any OPTIONAL CALL?

2 REPLIES 2

You could either wrap them in apoc.cypher.run() and collect their results internally.

Something else that should work is to use

so the first or 2nd result could result in no rows but the collect should then result in an empty collection (default value for no-rows aggregation).

call db.index.fulltext.queryNodes(...) yield node
with collect(node) as nodes
call db.index.fulltext.queryNodes(...) yield node
UNWIND collect(node) + nodes as n
RETURN n ORDER BY n.foo

Cases where there are no results may be problematic. I don't think we have an optional mode for the fulltext query, so using apoc.cypher.run() to perform the UNION would probably be best, then work with the returned results from the call.

Note that there are Cypher enhancements underway for the upcoming Neo4j 4.0 at the end of the year, so subquery support should allow for a similar approach without requiring an APOC call.