Neo4j节点类型各不相同的所有排列

jgwigjjp  于 2022-10-21  发布在  其他
关注(0)|答案(1)|浏览(138)

我要做的是生成节点集合的所有排列,但每个节点类型在每个排列中只出现一次。
例如,如果我有一个由u:User、l:Location、d:Device Nodes组成的图,假设每种类型有3个节点。
我希望能够生成如下所示的集合

User ID: 1, Location ID: 1
User ID: 1, Location ID: 2
User ID: 1, Location ID: 3
User ID: 1, Device ID: 1
User ID: 1, Device ID: 2
User ID: 1, Device ID: 3
User ID: 1, Location ID: 1, Device ID: 1
User ID: 1, Location ID: 1, Device ID: 2
User ID: 1, Location ID: 1, Device ID: 3
User ID: 1, Location ID: 2, Device ID: 1
User ID: 1, Location ID: 2, Device ID: 2
User ID: 1, Location ID: 2, Device ID: 3

以此类推,直到我有了所有的组合。
我不希望发生的情况是,一个有效的组合多次包含任何节点类型,因此一个组合只能有一个用户、一个位置和一个设备。
目前,我正在使用apoc.col.组合,但我似乎想不出一种方法来阻止它使节点类型不同。
如果我要参选

MATCH (l:Location)-[]-(d:Device)-[]-(ur:User)
WITH COLLECT({l:l,ur:ur}) as coll
WITH apoc.coll.combinations(coll,1,size(coll)) as combColl
RETURN combColl

我会耗尽内存,因为有效的组合将是所有的位置和所有的用户风险。

w80xi6nr

w80xi6nr1#

你需要为每个地点、设备和用户使用apoc.Coll.Companies获取一种物品组合。然后展开以组合每一项。结果是3x3+3x3+3x3+3x3x3=54行

MATCH (l:Location)
MATCH (d:Device)
MATCH (u:User)
WITH  collect(l) as locations, collect(d) as devices, collect(u) as users
WITH apoc.coll.combinations(locations,1,1) as locations,  apoc.coll.combinations(devices,1,1) as devices, apoc.coll.combinations(users,1,1) as users 
UNWIND locations as location
UNWIND devices as device
UNWIND users as user
CALL {
       WITH location, device 
       RETURN collect(distinct  location+device) as cols 

       UNION ALL

       WITH location, user
       RETURN collect(distinct  location+user) as cols 

       UNION ALL

       WITH device, user
       RETURN collect(distinct  device+user) as cols

       UNION ALL

       WITH location, device, user
       RETURN collect(distinct  location+device+user) as cols
}
RETURN collect(distinct cols) as combColl

当我展开最终集合时的结果:

╒══════════════════════════════╕
│"combColl"                    │
╞══════════════════════════════╡
│[[{"ID":1},{"ID":1}]]         │
├──────────────────────────────┤
│[[{"ID":1},{"ID":2}]]         │
├──────────────────────────────┤
│[[{"ID":1},{"ID":3}]]         │

....

├──────────────────────────────┤
│[[{"ID":3},{"ID":2},{"ID":3}]]│
├──────────────────────────────┤
│[[{"ID":3},{"ID":3},{"ID":1}]]│
├──────────────────────────────┤
│[[{"ID":3},{"ID":3},{"ID":2}]]│
├──────────────────────────────┤
│[[{"ID":3},{"ID":3},{"ID":3}]]│
└──────────────────────────────┘

相关问题