Creating nodes from a COLLECT


(Vsaran) #1

Reading a "Learning Neo4j" book from Packt publications. Found a query:

match (p:Person)-[b:BOUGHT]->(prod1:Product)-[:MADE_BY]->(br:Brand)<-
[MADE_BY]-(prod2:Product)
with p, br, prod2, count(prod1) as NrOfBrandProducts
where not(p-[:BOUGHT]->prod2) and NrOfBrandProducts > 1
return p.name as Person, br.name as Brand, collect(prod2.name) as
RecommendedProducts
order by Person ASC;

Question: What is the best way to create a [:RECOMMED] relationship from Brand to all products in the collect(prod2)? The query is good but I need the result as a graph not records.
Is FOREACH an option?


(Michael Hunger) #2

but the brand is already connected to all those products? b/c it made them.

Do you mean the user? Here is one for the user.

match (p:Person)-[b:BOUGHT]->(prod1:Product)-[:MADE_BY]->
        (br:Brand)<-[:MADE_BY]-(prod2:Product)
with p, br, prod2, count(prod1) as NrOfBrandProducts
where not (p)-[:BOUGHT]->(prod2) and NrOfBrandProducts > 1
merge (prod1)-[:RECOMMENDED_TO]->(p)

(Vsaran) #3

Ah, yes. I mean person. This is very helpful Michael. Thank you so much!