在Neo4j中查找具有相同相关节点的节点

时间:2018-03-20 13:58:28

标签: neo4j cypher

我有一个包含科学论文的数据库和以下架构中的相应作者:

(a:Author {id:1})-[:WROTE]->(p:Paper {title:"XY"})<-[:WROTE]-(b:Author {id:2})

我想找到经常一起写在论文上的作者。

我开始通过以下方式查询作者列表的“协作计数”:

MATCH (a:Author)-[:WROTE]->(p:Paper)<-[:WROTE]-(b:Author)
WITH a, b, COUNT(p) AS count
ORDER BY count DESC
RETURN a.name, b.name, count
LIMIT 10

这导致:

| a.name | b.name | count | |----------|----------|-------| | Author A | Author B | 10 | | Author B | Author A | 10 | | Author C | Author D | 4 | | Auhtor D | Author C | 4 | | ... | ... | ... |

有没有办法删除重复项?

是否有可能显示一个图表,其中包含作者及其共同撰写的相关论文?

编辑:我使用了以下查询,但我不确定它是否返回了希望的结果:

MATCH (a:Author)-[:WROTE]->(p:Paper)<-[:WROTE]-(b:Author)
WITH a, b, collect(p) AS paper, COUNT(p) AS count
ORDER BY count DESC
LIMIT 10
RETURN a, b, paper

1 个答案:

答案 0 :(得分:1)

要删除重复项,您可以添加WHERE子句,如下所示:

MATCH (a:Author)-[:WROTE]->(p:Paper)<-[:WROTE]-(b:Author)
WHERE id(a) > id(b)
WITH a, b, COUNT(p) AS count
ORDER BY count DESC
RETURN a.name, b.name, count
LIMIT 10