5
votes

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.

1
please describe more detailed and share what have you done. A vague answer to a vague question would be, if you want to use data from previous query in the next one, save the intermediate step to Neo4j.Tomaž Bratanič
@Tomaž Bratanič Sorry, I updated the description, I hope it is more clear now.Afshin

1 Answers

11
votes

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).