密码查询与至少两个其他节点相关的一对节点?

时间:2018-10-14 20:15:15

标签: neo4j cypher

我有一个问题,我需要找到一对相同节点类型都连接到的节点数。

如此
(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);

我想提取的信息是电影中一对演员在一起,以及一对演员中有#部电影。

2 个答案:

答案 0 :(得分:2)

1。创建图形

关于其他信息和要求,我想向您推荐一个适应图,它考虑了ActorMovie的不同作用:

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);

example graph

2。实现

2.1要求I

  

“我要提取的信息是   一起看电影[...]。

2.1.1解决方案

MATCH
  (movie:Movie)<-[:REFERS]-(actor:Actor)
RETURN
  movie.name AS movieName, collect(actor.name) AS actorTeam;

2.1.2结果

╒═══════════╤═════════════════════════╕
│"movieName"│"actorTeam"              │
╞═══════════╪═════════════════════════╡
│"Movie 2"  │["Alice","Bob"]          │
├───────────┼─────────────────────────┤
│"Movie 1"  │["Alice","Bob","Charlie"]│
├───────────┼─────────────────────────┤
│"Movie 4"  │["Charlie"]              │
├───────────┼─────────────────────────┤
│"Movie 3"  │["Charlie"]              │
└───────────┴─────────────────────────┘

2.2要求II

  

“我要提取的信息是[...]个电影的计数   一对演员进来了。”

2.2.1解决方案

MATCH
  (movie:Movie)<-[:REFERS]-(actor:Actor)
WITH
  movie.name AS movieName, collect(actor.name) AS actorTeam
RETURN
  actorTeam, count(actorTeam) AS movieAmount;

2.2.2结果

╒═════════════════════════╤═════════════╕
│"actorTeam"              │"movieAmount"│
╞═════════════════════════╪═════════════╡
│["Alice","Bob","Charlie"]│1            │
├─────────────────────────┼─────────────┤
│["Charlie"]              │2            │
├─────────────────────────┼─────────────┤
│["Alice","Bob"]          │1            │
└─────────────────────────┴─────────────┘


[扩展名]

2.3要求A(摘自评论)

  

“'alice,bob':2,'charlie,alice':1,'charlie,bob'1(   一位演员没关系)“

2.3.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比较避免了每对具有两个关系(每个方向一个),并避免了从节点到自身的关系。

2.3.2结果

╒════════════════╤══════════════╤═════════════╕
│"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);

example graph

解决方案

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           │
└───────────────┴────────────────┴────────────┘