Graphdb/Neo4j与另一个关系的关系,或与3个节点的关系 [英] Graphdb/Neo4j relationship to another relationship, or relationship with 3 nodes
问题描述
我想在Neo4j中制作IMDB的角色/角色结构.我需要标签Person
,Movie
和Character
. Character
,因为一个角色可以在多部电影中由不同的人扮演.
I want to make IMDB's characters/roles structure in Neo4j. I'll need labels Person
, Movie
and Character
. Character
, because a character can be in multiple movies, played by different people.
没有Character
,这很容易:
(Person)-[:PLAYS_IN]->(Movie)
但是PLAYS_IN
是Character
,因此它类似于:
But PLAYS_IN
is a Character
, so it would be something like:
(Person)-[:PLAYS_AS]->(Character)-[:PLAYS_IN]->(Movie)
,但这不起作用,因为它没有直接的Person-Movie
关系.没有这个,所有曾经扮演彼得·帕克的人都会出现在每部拥有彼得·帕克的电影中.
but that doesn't work, because it doesn't have a direct Person-Movie
relationship. Without that, everyone who ever played Peter Parker, is in every movie that has a Peter Parker.
必须存在一个人与电影的关系,但也必须有一个人与电影-字符的关系.如何?这可能有效,但这很讨厌:
There must be a Person-Movie relationship, but also a Person-Movie-Character relationship. How? This could work, but that's just nasty:
(Person)-[:PLAYS_IN {uuid}]->(Movie), (Character {uuid})
因为现在我正在创建自己的外键类型的关系.那是非常ungraphdb的.但这有效:
because now I'm creating my own foreign key kind of relationship. That's very ungraphdb. But it works:
MATCH (p:Person)-[r:PLAYS_IN]->(m:Movie), (c:Character)
WHERE c.uuid = r.uuid
RETURN p, c, m
通过构建笛卡尔乘积=(这是非常RDBMS,但不是非常的graphdb.而且我无法查询Character-Movie
或Character-Person
,因为这不是真正的关系.
by building a cartesian product =( which is very RDBMS, but not very graphdb. And I can't query Character-Movie
or Character-Person
, because that's not a real relationship.
如何在Neo4j中使用3个外键(movie_id, character_id, person_id
)创建RDBMS链接表?
How do I make a RDBMS link table with 3 foreign keys (movie_id, character_id, person_id
) in Neo4j??
修改1
RDBMS等效项:
edit 1
The RDBMS equivalent:
movies (id, title) # e.g. Dragon Tattoo, or Spider's Web
people (id, name) # e.g. Rooney Mara, or Claire Foy
characters (id, name) # e.g. Lisbeth Salander
roles (movie_id, person_id, character_id) # 2 rows with 1 distinct character_id
推荐答案
超图.但是由于neo4j不支持超图,因此您需要对其建模.例如:
Your problem is solved by hypergraphs. But since neo4j does not support hypergraphs, you need to model them. For example:
CREATE (P1:Person {name: 'Tobey Maguire'})
CREATE (P2:Person {name: 'Nicholas Hammond'})
CREATE (CW1:CreativeWork {name: 'Spider-Man'})
CREATE (CW2:CreativeWork {name: 'The Amazing Spider-Man (TV Series)'})
CREATE (CH:Character {name: 'Spider-Man'})
CREATE (A1:Role)
CREATE (A2:Role)
CREATE (P1)-[:PLAYS_AS]->(A1)
CREATE (A1)-[:HAS_CHARACTER]->(CH)
CREATE (A1)-[:PLAYS_IN]->(CW1)
CREATE (P2)-[:PLAYS_AS]->(A2)
CREATE (A2)-[:HAS_CHARACTER]->(CH)
CREATE (A2)-[:PLAYS_IN]->(CW2)
这篇关于Graphdb/Neo4j与另一个关系的关系,或与3个节点的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!