聚合多对多关系的 Cypher 虚拟节点

Cypher virtual nodes to aggregate many to many relationships

我有 8000 个作者节点和 2000 个图书节点。每个作者都有一个国家属性,每本书可以由许多作者合着。

我试图通过将一个国家/地区的所有作者分组到一个名为 Country 的虚拟节点中来显示哪些国家/地区在书籍上进行了合作。然后,我需要根据 CountryA 中每位作者与 CountryB 中的作者发表作品的次数,在国家/地区之间创建虚拟关系。

我有一些查询正在做一些事情,但不能完全达到我想要的效果。这是我尝试过的方法和结果:

MATCH (a1:Author)-->(p:Paper)<--(a2:Author)
WHERE a1.country <> a2.country
WITH apoc.create.vNode(['Country'], {name: a1.country}) as Country1,  apoc.create.vNode(['Country'], {name: a2.country}) as Country2, count(p) as numCollabs
RETURN DISTINCT Country1, Country2, apoc.create.vRelationship(Country1, 'COLLABORATED_WITH', {numCollabs: numCollabs}, Country2) LIMIT 25

这显示了很多重复的国家(我假设每个作者 1 个而不是汇总作者)并且每个关系的 numCollabs 总是 1。

我也试过这个:

MATCH (a: Author)
WITH a.country as country, count(*) as count
RETURN apoc.create.vNode(['Country'], {name: country, authors: count}) as countries

这为我提供了适当数量的国家并显示了该国家/地区的作者数量...但是,我不知道如何创建 vRelationship 以显示每个国家/地区相互合作的次数国家。

试试这个。我尝试根据您的数据调整示例 here

MATCH (a:Author) 
WITH collect(distinct a.country) as countries
WITH [cName in countries | apoc.create.vNode(['Country'],{name:cName})] as countryNodes
WITH apoc.map.groupBy(countryNodes,'name') as countries
MATCH (a1:Author)-->(p:Paper)<--(a2:Author)
WHERE a1.country < a2.country
WITH a1.country AS countryName1, 
a2.country as countryName2, 
count(distinct p) as numCollabs, countries
RETURN countries[countryName1] as country1, 
countries[countryName2] as country2, 
apoc.create.vRelationship(countries[countryName1], 'COLLABORATED_WITH', {numCollabs: numCollabs}, countries[countryName2]) LIMIT 25