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.

Getting error when running condition on other nodes

Hello,

I am trying to get all the Restaurants that User has not yet reviewed based on the Category condition but I am getting the following error:

ERROR:

In a WITH/RETURN with DISTINCT or an aggregation, it is not possible to access variables declared before the WITH/RETURN: user (line 3, column 12 (offset: 241))

HERE IS MY QUERY:

> MATCH(user:User{user_id:"u-Gcx8OEYpT85qddaoiFw3Gw"})-[:WROTE]->(review:Review)-[:REVIEWS_FOR]->(rest1:Restaurant)-[c1:IN_CATEGORY]->(cat:Category)-[c2:IN_CATEGORY]-(rest2:Restaurant) 
> WITH AVG(toFloat(review.stars)) AS avg_rating
> WHERE NOT (user)--(rest2) AND review.stars > avg_rating  AND rest2.stars > avg_rating 
> RETURN rest2 LIMIT 15
1 ACCEPTED SOLUTION

You should convert to float all your numbers.

Otherwise:

MATCH (user:User{user_id:"u-Gcx8OEYpT85qddaoiFw3Gw"})-[:WROTE]->(review:Review)-[:REVIEWS_FOR]->(rest1:Restaurant)-[c1:IN_CATEGORY]->(cat:Category)-[c2:IN_CATEGORY]-(rest2:Restaurant) 
WHERE NOT (user)--(rest2) AND toFloat(rest2.stars) > 3.5 AND toFloat(rest2.review_count) > 50
RETURN rest2 LIMIT 15

View solution in original post

5 REPLIES 5

Hello @engr.muzamilshah

It's because user, rest2, review and rest2 must be specified in the previous WITH clause to be used.

An easy and dirty solution:

MATCH (user:User{user_id:"u-Gcx8OEYpT85qddaoiFw3Gw"})-[:WROTE]->(review:Review) 
WITH AVG(toFloat(review.stars)) AS avg_rating
MATCH (user:User{user_id:"u-Gcx8OEYpT85qddaoiFw3Gw"})-[:WROTE]->(review:Review)-[:REVIEWS_FOR]->(rest1:Restaurant)-[c1:IN_CATEGORY]->(cat:Category)-[c2:IN_CATEGORY]-(rest2:Restaurant)
WHERE NOT (user)--(rest2)
AND review.stars > avg_rating
AND rest2.stars > avg_rating 
RETURN rest2 LIMIT 15

Regards,
Cobra

Thank you so much for your fast response.

Can you please figure out what is the problem in this query..

MATCH(user:User{user_id:"u-Gcx8OEYpT85qddaoiFw3Gw"})-[:WROTE]->(review:Review)-[:REVIEWS_FOR]->(rest1:Restaurant)-[c1:IN_CATEGORY]->(cat:Category)-[c2:IN_CATEGORY]-(rest2:Restaurant) 
WHERE NOT (user)--(rest2) AND rest2.stars > "3.5" AND rest2.review_count > "50"
RETURN rest2 LIMIT 15

This condition is not working...
rest2.review_count > "50"

As you can see it lists up also those restaurant which have review count less than 50.
Thanks

You should convert to float all your numbers.

Otherwise:

MATCH (user:User{user_id:"u-Gcx8OEYpT85qddaoiFw3Gw"})-[:WROTE]->(review:Review)-[:REVIEWS_FOR]->(rest1:Restaurant)-[c1:IN_CATEGORY]->(cat:Category)-[c2:IN_CATEGORY]-(rest2:Restaurant) 
WHERE NOT (user)--(rest2) AND toFloat(rest2.stars) > 3.5 AND toFloat(rest2.review_count) > 50
RETURN rest2 LIMIT 15

Thank you so much.

Your assistance is really appreciable.