我有一个问题,我需要找到一对相同节点类型都连接到的节点数。
如此
(a1)-(b)
(a2)-(b)
(a1)-(c)
(a2)-(c)
我想要的结果是:
a1,a2、2
[我正在查看的那对节点]并计算它们都与多少其他注释相关。
到目前为止,我有一个查询,该查询可为我提供所需的节点,但是对于如何形成一个“对”,然后可以在其他相关节点上进行搜索,我感到困惑。
添加示例:
创建
(a1:Node {name:'Alice'})-[:REFERS]->(b:Node {name:'movie1'}),
(a2:Node {name:'Bob'})-[:REFERS]->(b),
(a3:Node {name:'Charlie'}})-[:REFERS]->(b),
(a3)-[:REFERS]->(d:Node {name:'movie3'}),
(a3)-[:REFERS]->(e:Node {name:'movie4'}),
(a1)-[:REFERS]->(c:Node {name:'movie2'}),
(a2)-[:REFERS]->(c);
我想提取的信息是电影中一对演员在一起,以及一对演员中有#部电影。
答案 0 :(得分:2)
关于其他信息和要求,我想向您推荐一个适应图,它考虑了Actor
和Movie
的不同作用:
CREATE
(alice:Actor {name: 'Alice'})-[:REFERS]->(movie1:Movie {name: 'Movie 1'}),
(bob:Actor {name: 'Bob'})-[:REFERS]->(movie1),
(charlie:Actor {name: 'Charlie'})-[:REFERS]->(movie1),
(charlie)-[:REFERS]->(movie3:Movie {name: 'Movie 3'}),
(charlie)-[:REFERS]->(movie4:Movie {name: 'Movie 4'}),
(alice)-[:REFERS]->(movie2:Movie {name: 'Movie 2'}),
(bob)-[:REFERS]->(movie2);
“我要提取的信息是 一起看电影[...]。
MATCH
(movie:Movie)<-[:REFERS]-(actor:Actor)
RETURN
movie.name AS movieName, collect(actor.name) AS actorTeam;
╒═══════════╤═════════════════════════╕
│"movieName"│"actorTeam" │
╞═══════════╪═════════════════════════╡
│"Movie 2" │["Alice","Bob"] │
├───────────┼─────────────────────────┤
│"Movie 1" │["Alice","Bob","Charlie"]│
├───────────┼─────────────────────────┤
│"Movie 4" │["Charlie"] │
├───────────┼─────────────────────────┤
│"Movie 3" │["Charlie"] │
└───────────┴─────────────────────────┘
“我要提取的信息是[...]个电影的计数 一对演员进来了。”
MATCH
(movie:Movie)<-[:REFERS]-(actor:Actor)
WITH
movie.name AS movieName, collect(actor.name) AS actorTeam
RETURN
actorTeam, count(actorTeam) AS movieAmount;
╒═════════════════════════╤═════════════╕
│"actorTeam" │"movieAmount"│
╞═════════════════════════╪═════════════╡
│["Alice","Bob","Charlie"]│1 │
├─────────────────────────┼─────────────┤
│["Charlie"] │2 │
├─────────────────────────┼─────────────┤
│["Alice","Bob"] │1 │
└─────────────────────────┴─────────────┘
“'alice,bob':2,'charlie,alice':1,'charlie,bob'1( 一位演员没关系)“
MATCH
(startActor:Actor)-[:REFERS]->(movie:Movie)<-[:REFERS]-(endActor:Actor)
WHERE
id(startActor) < id(endActor)
WITH
startActor.name AS startActorName, endActor.name AS endActorName, count(movie) AS movieAmount
RETURN
startActorName, endActorName, movieAmount;
第4行的id
子句中的WHERE
比较避免了每对具有两个关系(每个方向一个),并避免了从节点到自身的关系。
╒════════════════╤══════════════╤═════════════╕
│"startActorName"│"endActorName"│"movieAmount"│
╞════════════════╪══════════════╪═════════════╡
│"Alice" │"Bob" │2 │
├────────────────┼──────────────┼─────────────┤
│"Alice" │"Charlie" │1 │
├────────────────┼──────────────┼─────────────┤
│"Bob" │"Charlie" │1 │
└────────────────┴──────────────┴─────────────┘
答案 1 :(得分:1)
为便于进一步解答和解决方案,我注意到了我的图形创建语句:
CREATE
(a1:Node {name: 'a1'})-[:REFERS]->(b:Node {name: 'b'}),
(a2:Node {name: 'a2'})-[:REFERS]->(b),
(a3:Node {name: 'a3'})-[:REFERS]->(b),
(a1)-[:REFERS]->(c:Node {name: 'c'}),
(a2)-[:REFERS]->(c);
MATCH
(startNode:Node)-[:REFERS]-(endNode:Node)
WITH
startNode.name AS inspectedNode, collect(endNode.name) AS pair
RETURN
inspectedNode, pair, size(pair) AS pairAmount;
╒═══════════════╤════════════════╤════════════╕
│"inspectedNode"│"pair" │"pairAmount"│
╞═══════════════╪════════════════╪════════════╡
│"a2" │["b","c"] │2 │
├───────────────┼────────────────┼────────────┤
│"c" │["a1","a2"] │2 │
├───────────────┼────────────────┼────────────┤
│"b" │["a1","a2","a3"]│3 │
├───────────────┼────────────────┼────────────┤
│"a1" │["b","c"] │2 │
├───────────────┼────────────────┼────────────┤
│"a3" │["b"] │1 │
└───────────────┴────────────────┴────────────┘