MERGE the results/list in Neo4j using Cypher

cypher, neo4j

Solution

The trick I learned somewhere to make this work is

MATCH (:class1)-->(c1:class2)-->(:class3)--(:class4)-->(c2:class2)
WITH collect(c1)+collect(c2) as nodez
UNWIND nodez as c
RETURN c

Note that you can't combine lists of different types (eg. nodes+relationships) this way. They have to all be the same type (eg. all nodes or all relationships). If you want to mix types in an aggregate list, you will need to convert everything to be the same type first (probably map).

Problem

Imagine my query results in nodes with different names, but in my next query, I want to search in the merged version of the previous result. How can I merge two lists or two set of nodes? As an example, imagine I have (:class1)-->(c1:class2)-->(:class3)--(:class4)-->(c2:class2) and then I would like to do a MATCH based on the distinct elements in the merge of c1.name and c2.name.

Original source