cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Apoc.periodic.iterate creating multiple edges

I am trying to build recommended products per customer. I have a query that gets my recommended products BUT when executed in apoc.periodic.iterate I get multiple relationships. (I get the number of relationships in my LIMIT). I works when I LIMIT 1 and BATCH 1

CALL apoc.periodic.iterate("MATCH (c:customer)-[]->(p:product) WHERE EXISTS (c.age) AND NOT (c)<-[:RECOMMEND]-() RETURN c LIMIT 2","MATCH (c)-[]->(p:product)<-[]-(nc:customer)
WITH COLLECT (DISTINCT p) as prods, c, p, nc
WHERE c <> nc AND ALL(p in prods WHERE (p)<-[]-(nc)) AND nc.age > (c.age - 10) AND nc.age < (c.age + 10)
MATCH (nc)-[]->(np)-[]->(u:use)
WITH np, u, c, COLLECT(p.article_id) as arts
WHERE not np.article_id in arts
WITH  c, u.name as use, np.article_id as product, count(*) as score
ORDER BY score DESC
WITH c,  use, Collect(product)[0..1] as products
WITH c,  COLLECT(products)[0..12] as recomendation
UNWIND recomendation AS x
UNWIND x AS rec
MATCH (r:product{article_id: rec})
WITH c, r
CREATE (r)-[:RECOMMEND]->(c)",
{batchSize:2, parallel: true, iterateList:true})

LIMIT BATCH PARALLEL ITERATELIST DURATION
1 1 True true 5985 ms
1 1 true false 35822 ms.
1 1 False false 4864 ms.
1 1 False true 492 ms.
2 1 True true 2286 ms.
*double edges
2 1 true false 6227 ms
*double edges
2 1 False false 61752 ms.
double edges
2 1 False true 403 ms.
double edges
1 ACCEPTED SOLUTION

*Also here is the plan for the query. ANY help would be appreciated

View solution in original post

3 REPLIES 3

*Also here is the plan for the query. ANY help would be appreciated

If you are getting multiple of the same relationships being added to the same customer, I think the cause is in your first query. This query produces customers that your second query gets executed for each customer. Since a customer can have multiple products, the output of the first query can have duplicate customers.

You can fix that in either way:

MATCH (c:customer)-->(:product)
WHERE EXISTS (c.age)
AND NOT EXISTS ( (c)<-[:RECOMMEND]-() ) RETURN DISTINCT c

Or

MATCH (c:customer)
WHERE EXISTS (c.age)
AND EXISTS ( (c)-->(:product) )
AND NOT EXISTS ( (c)<-[:RECOMMEND]-() ) RETURN c