Strange results using UNION

Sure, the problem is that the variables used in UNIONed queries are independent. The recipe you match to in the first part of the UNION is not visible or reused in the second part, so in the second part is viewed as an entirely new variable recipe, not the specific one you matched to earlier. The second query will start from every :Mention node and return every node that has a traditional wine mentioned.

In order to reuse the variable, you should MATCH to the recipe outside of the subquery call and pass it in so it is visible from both parts of the UNION:

MATCH (recipe:Recipe {uuid: "ee35d995-bcac-46f9-aeda-2bffc7a29dfa"})
CALL {
    WITH recipe
    MATCH (recipe)<-[:CAN_BE_SUGGESTED_FOR]-(mention:Mention)
    RETURN mention, recipe
  UNION
    WITH recipe 
    MATCH (recipe)-[:HAS_TRADITIONAL_WINE]->(mention:Mention)
    RETURN mention, recipe
}
RETURN mention, recipe
1 Like