我不知道怎么说这个问题,因为我甚至不知道它叫什么,但我会尝试。
你可以想象我的neo4j实例中有以下图形表示和关系:
我想问Neo4j的问题如下:什么是电影 仅代理商-A演员采取行动?
这是一个简单的样本。就我而言,我有成千上万的演员加入了一个经纪人和数千部演员可能扮演的电影。
PS:如果你能告诉我我想要做的事情的名字,我也很感激:)
我能够有一些稳固的工作。这是数据集:
CREATE (AgencyA:Agency {name: 'Agency-A'})
CREATE (Actor1:Person {name: 'Actor-1'})
CREATE (Actor2:Person {name: 'Actor-2'})
CREATE (Actor3:Person {name: 'Actor-3'})
CREATE (Actor4:Person {name: 'Actor-4'})
CREATE (Actor5:Person {name: 'Actor-5'})
CREATE
(AgencyA)-[:ACQUIRED]->(Actor1),
(AgencyA)-[:ACQUIRED]->(Actor3),
(AgencyA)-[:ACQUIRED]->(Actor5)
CREATE (MovieA:Movie {name: "Movie-A"})
CREATE (MovieB:Movie {name: "Movie-B"})
CREATE (MovieC:Movie {name: "Movie-C"})
CREATE
(MovieA)-[:EMPLOYED]->(Actor1),
(MovieA)-[:EMPLOYED]->(Actor5),
(MovieB)-[:EMPLOYED]->(Actor1),
(MovieB)-[:EMPLOYED]->(Actor3),
(MovieB)-[:EMPLOYED]->(Actor5),
(MovieC)-[:EMPLOYED]->(Actor2),
(MovieC)-[:EMPLOYED]->(Actor5)
以下是查询:
MATCH (agency:Agency {name:"Agency-A"})-[:ACQUIRED]->(actor:Person)<-[:EMPLOYED]-(movie:Movie)
RETURN distinct movie.name
结果:
电影-C
电影-B
电影-A
这不是我想要的。我感兴趣的是只有代理商A演员受雇的电影,而不是任何其他演员。所以,结果应该是:
电影-A
电影-B
答案 0 :(得分:3)
在Cypher中,这很简单:)
MATCH (agency:Agency {name:"AgencyA"})<-[:JOINED]-(actor:Actor)-[:ACTED_IN]->(movie:Movie)
RETURN distinct movie.name
如果演员可能多次加入同一个代理机构(因此与同一代理机构有多个JOINED
关系),您可以在连接电影之前将其区分开来:
MATCH (agency:Agency {name:"AgencyA"})<-[:JOINED]-(actor:Actor)
WITH distinct actor as actor
MATCH (actor)-[:ACTED]->(movie:Movie)
RETURN distinct movie.name
<强>更新强>
在澄清没有其他人可以在电影中播放代理人A的人之后,这是一个有效的查询:
MATCH (agency:Agency { name:"Agency-A" })-[:ACQUIRED]->(actor:Person)<-[:EMPLOYED]-(movie:Movie)
WITH DISTINCT movie, collect(actor) AS actors
MATCH (movie)-[:EMPLOYED]->(allemployees:Person)
WITH movie, actors, count(allemployees) AS c
WHERE c = size(actors)
RETURN movie.name
在这里,我们将电影员工总数与代理商首次匹配的员工人数进行比较
你可以在这里测试一下: