使用Cypher找到评分最低的最受欢迎的电影。合并查询

时间:2018-04-16 13:01:05

标签: graph neo4j cypher

我目前正在尝试使用电影的neo4j数据集示例创建查询。我想选择带有很多标签的电影(更受欢迎)以及评分较低的电影。

我已经弄清楚如何找到最受欢迎的(超过平均值):

MATCH (m:MOVIE)-[r:HAS_TAG]->() 
WITH count(r) as tagnum, m.title as title_now
WITH avg(tagnum) as avgtagnum
MATCH (m:MOVIE)-[r:HAS_TAG]->() 
WITH count(r) as tagnum, m.title as title_now, avgtagnum
WHERE tagnum > avgtagnum*3
RETURN title_now, avgtagnum, tagnum 
ORDER BY  tagnum DESC

还有如何获得少注意的那些

MATCH ()-[r:RATED]->(m:MOVIE) 
WITH  avg(r.rating) as avgrating 
MATCH ()-[r:RATED]->(m:MOVIE) 
WITH m.title as titlemovie, avgrating, avg(r.rating) as  movierating 
WHERE movierating < avgrating/2
RETURN titlemovie, avgrating, movierating 
ORDER BY movierating ASC

但是当我将它们合并在一起时,查询永远不会结束。

MATCH ()-[r:RATED]->(m:MOVIE) 
WITH  avg(r.rating) as avgrating 
MATCH ()-[r:RATED]->(m:MOVIE) 
WITH m.title as titlemovie, avgrating, avg(r.rating) as  movierating 
WHERE movierating < avgrating/2
MATCH (m:MOVIE)-[r:HAS_TAG]->() 
WITH count(r) as tagnum, m.title as title_now, titlemovie, avgrating, movierating 
WITH avg(tagnum) as avgtagnum, titlemovie, avgrating, movierating 
MATCH (m:MOVIE)-[r:HAS_TAG]->() 
WITH count(r) as tagnum, m.title as title_now, avgtagnum, titlemovie, avgrating, movierating 
WHERE tagnum > avgtagnum*3
RETURN titlemovie, avgrating, movierating, avgtagnum, tagnum 
ORDER BY tagnum DESC,  movierating ASC

问题是如何找到符合我要求的电影;少注意,更受欢迎?

1 个答案:

答案 0 :(得分:2)

  1. 首先,使用平均(评分)会给你平均评分而不是电影的平均评分。
  2. 2。 MATCH (m:MOVIE)-[r:HAS_TAG]->()
    WITH count(r) as tagnum, ... WITH avg(tagnum) as avgtagnum ...
      由于模式匹配,此代码不会为您提供完全平均的标记数量,只考虑那些带有某些标记的电影(因此您将获得比实际更大的平均数)。

    1. 为什么使用WITH ... m.title as title_now, titlemovie两个头衔?最好的猜测是你匹配错误的电影
    2. 如果必须纠正你的查询,我会这样写:

      MATCH ()-[r:RATED]->(m:MOVIE) 
      WITH  m, avg(r.rating) as avgra
      with avg(avgra) as avgrating
      MATCH (m:MOVIE) 
      WITH avg(size((m)-[:HAS_TAG]-())) as avgtagnum, avgrating
      MATCH (m:MOVIE)-[r:HAS_TAG]->() 
      WITH count(r) as tagnum, m.title as titlemovie, avgtagnum, avgrating, avg(r.rating) as movierating
      WHERE tagnum > avgtagnum*3 and movierating < avgrating/2
      RETURN titlemovie, avgrating, movierating, avgtagnum, tagnum 
      ORDER BY tagnum DESC,  movierating ASC