I have a query using UNWIND which then feeds an OPTIONAL MATCH, these are 
both based on a userId being passed through into the query

MATCH (movie:Movie {movieId}) 
OPTIONAL MATCH (me:User {id: {userId}})-[:SIMILAR]-(:User)-[rating:RATED]->(
movie)
UNWIND (CASE rating.recommend WHEN [] then [null] else rating.recommend end) 
AS ids
OPTIONAL MATCH (related:Movie) WHERE related.id = ids
WITH DISTINCT movie, related, count(related) AS countRelated ORDER BY 
countRelated DESC
RETURN DISTINCT movie, 
collect(DISTINCT {name:related.name, id:related.id, count:countRelated}) AS 
related,


When the query is provided a userId it works and returns related as I'd 
expect, when the userId is null or does not exist, the query returns no 
records, when I am expecting a distinct Movie to be returned and related to 
return null or [].  

Have tried FOREACH loops (below), but get the error cannot use MATCH inside 
FOREACH

MATCH (movie:Movie {movieId}) 
OPTIONAL MATCH (me:User {id: {userId}})
FOREACH (o IN CASE WHEN me.id EXISTS THEN [1] ELSE [] END |
OPTIONAL MATCH (me)-[:SIMILAR]-(:User)-[rating:RATED]->(movie)
UNWIND (CASE rating.recommend WHEN [] then [null] else rating.recommend end) 
AS ids
OPTIONAL MATCH (related:Movie) WHERE related.id = ids
WITH DISTINCT movie, related, count(related) AS countRelated ORDER BY 
countRelated DESC
RETURN DISTINCT movie, 
collect(DISTINCT {name:related.name, id:related.id, count:countRelated}) AS 
related
)
FOREACH (o IN CASE WHEN me.id IS NULL THEN [1] ELSE [] END | 
RETURN DISTINCT movie, 
)


Have tried to put a case around the related match like this, but gives me 
an invalid input 'W' expected whitespace....  error 



MATCH (movie:Movie {id: movieId})
OPTIONAL MATCH (me:User {id: {userId})
OPTIONAL MATCH 
(CASE WHEN me.id EXISTS then 
(me)-[:SIMILAR]-(:User)-[rating:RATED]->(movie)
ELSE 
()-[:SIMILAR]-(:User)-[rating:RATED]->(movie)
END)
UNWIND rating.recommend AS ids
OPTIONAL MATCH (related:Whisky) WHERE related.id = ids
WITH DISTINCT movie related, count(related) AS countRelated ORDER BY 
countRelated DESC
RETURN DISTINCT movie, 
collect(DISTINCT {name:related.name, id:related.id, count:countRelated}) AS 
related

How can I return distinct movie regardless of the userId, being either null 
or not matching an Id.?

-- 
You received this message because you are subscribed to the Google Groups 
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to