I have a doubt in query 4 of this article here Can someone please explain what line no. 6 and 7 are doing.
MATCH (c:Customer)-[:PURCHASED]->(o:Order)-[:PRODUCT]->(p:Product)
WITH c, count(p) AS total
MATCH (c)-[:PURCHASED]->(o:Order)-[:PRODUCT]->(p:Product)
WITH c, total,p, count(o)*1.0 AS orders
MERGE (c)-[rated:RATED]->(p)
ON CREATE SET rated.rating = orders/total
ON MATCH SET rated.rating = orders/total
WITH c.companyName AS company, p.productName AS product, orders, total, rated.rating AS rating
ORDER BY rating DESC
RETURN company, product, orders, total, rating LIMIT 10
Here's the documentation for ON CREATE and ON MATCH, which are clauses you can only use directly following a MERGE clause.
MERGE is like a MATCH of a pattern, and then if the pattern doesn't exist, it will perform a CREATE, so this allows some conditional behavior on setting properties depending upon the result of the MERGE.
In this particular case, the same operation is being performed no matter if the MERGE only needed to MATCH to an existing pattern, or CREATE it. So ideally you wouldn't use either of these, you would just use SET
after the MERGE.
Only when the behavior needs to differ between one case and the other would you want to use these in your query.