问题
I am trying to get top 10 users who have liked particular game with game_id from 1 to 25. These games have a relationship with user called rating with property rating_val=1 to 10 .
How to get 25 rows with group of all users who have rating_val from 1 to 10 desc order for each game category.
Basically :
25 game categories with id 1 to 25
games_like is a relationship with with rating_val from 1-10
users are nodes with id,name
This query is not working:
MATCH (u:user { user_id:"1" })
MATCH (o:user)
WHERE o <> u
OPTIONAL MATCH (u)-[r:games_like]->(d)<-[rw:games_like]-(o)
RETURN
toInt(r.rating_val)+toInt(rw.rating_val) as sum ,
collect(DISTINCT (r.rating_val)) AS user1,
collect(DISTINCT (rw.rating_val)) AS user2,
d
ORDER BY sum DESC
回答1:
I'm a bit confused because your description seems to be saying you just want a join between users and games, but your query is matching two users against one game. Also, do your game nodes have a label?
From your description, this is the query I would write:
MATCH (game)<-[rel:games_like]-(user:user)
WITH game, user ORDER BY rel.rating_val DESC
RETURN game, collect(user)[0..10]
That returns you the top users for each game. If you want to limit it you could do:
MATCH (game)<-[rel:games_like]-(user:user)
WHERE
1 <= game.game_id AND game.game_id <= 25 AND
1 <= rel.rating_val AND rel.rating_val <= 10
WITH game, user ORDER BY rel.rating_val DESC
RETURN game, collect(user)[0..10]
来源:https://stackoverflow.com/questions/30353205/top-10-users-for-each-game-in-neo4j-based-of-games-they-like