简体   繁体   English

Neo4j Cypher “至少 2 个导演和至少 5 个演员”

[英]Neo4j Cypher "at least 2 directors and at least 5 actors"

Which film has at least 2 directors and at least 5 actors?哪部电影至少有 2 个导演和至少 5 个演员? So far got this but it gives me the wrong answer.到目前为止得到了这个,但它给了我错误的答案。 It gives too many movies and counts.它提供了太多的电影和计数。

MATCH (a:Person)-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(d:Person) WITH m, count(a) AS numAct, count(d) AS numDir WHERE numAct >= 5 AND numDir >= 2 RETURN  m.title, numDir AS director_count, numAct AS actor_count

What's wrong with it?它出什么问题了?

The way the aggregation part works is that it groups by the movie node 'm'.聚合部分的工作方式是按电影节点“m”进行分组。 In the part,在部分,

...WITH m, count(a) AS numAct, count(d) AS numDir...

it will collect the movies, actors and directors as separate rows, one row for each combination of movie, actor and director and then counts the rows.它将电影、演员和导演收集为单独的行,电影、演员和导演的每个组合占一行,然后对行进行计数。 If you run,如果你跑,

MATCH (a:Person)-[:ACTED_IN]->(m:Movie)<-[:DIRECTED]-(d:Person) 
RETURN m, a, d

you will see exactly the rows that the count aggregation will work on您将准确地看到计数聚合将处理的行

To solve your problem, you will have to update the cypher, breaking up the aggregation.要解决您的问题,您必须更新密码,分解聚合。 One way is,一种方法是,

MATCH (a:Person)-[:ACTED_IN]->(m:Movie) WITH m, count(a) AS numAct
MATCH (m)<-[:DIRECTED]-(d:Person) WITH m, numAct, count(d) as numDir
WHERE numAct >= 5 AND numDir >= 2 
RETURN  m.title, numDir AS director_count, numAct AS actor_count

Updated after @Graphileon's observation在@Graphileon 观察后更新

I guess the shortest way is我想最短的方法是

MATCH (m:Movie)
WHERE size([(m)<-[:DIRECTED]-(p:Person) | p]) >= 2
      AND size([(m)<-[:ACTED_IN]-(p:Person) | p]) >= 5
RETURN m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM