Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MERGE the results/list in Neo4j using Cypher

Tags:

neo4j

cypher

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.

like image 403
Afshin Avatar asked May 01 '17 22:05

Afshin


1 Answers

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

like image 74
Tezra Avatar answered Nov 15 '22 05:11

Tezra