使用Cypher查询Neo4j电影数据集:高标签量低评分影片合并查询
Got it, let's tackle this. You already have the logic to filter high-tag-count movies (over 3x the average tag count), and now we just need to fold in the low-rating condition. Here's how to combine both requirements efficiently, using Neo4j's movie dataset where :MOVIE nodes have a rating property:
Full Cypher Query
// Calculate average tag count and average rating first (single pass for efficiency) MATCH (m:MOVIE)-[r:HAS_TAG]->() WITH count(r) AS tagnum, m.rating AS rating WITH avg(tagnum) AS avgtagnum, avg(rating) AS avgrating // Match movies again, filter for high tag count AND low rating MATCH (m:MOVIE)-[r:HAS_TAG]->() WITH m, count(r) AS tagnum, avgtagnum, avgrating WHERE tagnum > avgtagnum * 3 AND m.rating < avgrating // Use average rating as "low" threshold; replace with fixed value like 5 if preferred RETURN m.title AS title, tagnum AS tag_count, m.rating AS rating ORDER BY tagnum DESC, m.rating ASC
Breakdown of the Logic
- First Block: We first iterate over all tagged movies to compute two key stats in one go: the average number of tags per movie (
avgtagnum) and the average rating across all tagged movies (avgrating). This avoids redundant database hits compared to calculating them separately. - Second Block: We re-match tagged movies, count their tags, and apply your original high-tag filter plus the low-rating check. Using the average rating as the threshold keeps the "low rating" relative to the dataset, but you can swap
m.rating < avgratingfor a fixed number likem.rating < 5if you want a hard cutoff. - Final Return: We output the movie title, tag count, and rating, sorted to show the most tagged (hottest) low-rated movies first.
Quick Adjustment Note
If some movies in your dataset don't have a rating property, you can add AND exists(m.rating) to the WHERE clause to exclude those, so you only get movies with valid ratings.
内容的提问来源于stack exchange,提问作者guifreballester
相关产品推荐
相关产品推荐

