多个MATCH查询中的不同节点

时间:2014-10-03 08:58:13

标签: neo4j cypher

我有一个电影数据库,用户评价电影。我想找到用户1的前5个最相似的用户(第一个MATCH工作正常),并向他推荐那些类似用户观看但未被用户1观看的评价最高的电影。即使我有多次,我也会多次获得相同的电影"不同"在我的查询中。我做错了什么?

MATCH (target_user:User {id : 1})-[:RATED]->(m:Movie)
      <-[:RATED]-(other_user:User)
WITH other_user, count(distinct m.title) AS num_common_movies, target_user
ORDER BY num_common_movies DESC
LIMIT 5
MATCH other_user-[rat_other_user:RATED]->(m2:Movie)
WHERE NOT (target_user-[:RATED]->m2)
WITH distinct m2.title as movietitle, rat_other_user.note AS rating,
       other_user.id AS watched_by
RETURN movietitle, rating, watched_by
ORDER BY rating DESC

1 个答案:

答案 0 :(得分:2)

您的数据集可能有很多用户观看并评分了相同的电影。当您执行该DISTINCT语句时,它将返回一个不同的行,而不是一个独特的电影标题。不同的用户会对未观看的电影进行不同的评分,并且名称不同。

您必须针对特定用例对此进行调整,但您可以从以下开始:

MATCH (target_user:User { uid : 1 })-[:RATED]->(m:Movie)<-[:RATED]-(other_user:User)
WITH other_user, count(DISTINCT m.title) AS num_common_movies, target_user
ORDER BY num_common_movies DESC 
LIMIT 5
MATCH other_user-[rat_other_user:RATED]->(m2:Movie)
WHERE NOT (target_user-[:RATED]->m2)
RETURN DISTINCT m2.name AS movietitle, COLLECT(rat_other_user.note) AS ratings, 
MAX(rat_other_user.note) AS maxi, AVG(rat_other_user.note) as aver, COLLECT(other_user.name) AS users
ORDER BY aver DESC

我添加了一个控制台演示here

重要的是,您现在正在汇总每个电影标题的结果。