我有以下代码来初始化数据库
create
(C1: Company{name:'Company A'}),
(C2: Company{name:'Company B'}),
(C3: Company{name:'Company C'}),
(C4: Company{name:'Company D'}),
(C1)-[:Sell{contract:"TA1801"}]->(C2),
(C2)-[:Sell{contract:"TA1802"}]->(C3),
(C3)-[:Sell{contract:"TA1803"}]->(C1),
(C3)-[:Sell{contract:"TA1804"}]->(C4),
(C1)-[:Sell{contract:"TA1805"}]->(C4),
(C4)-[:Sell{contract:"TA1806"}]->(C1)
比方说,我只想找到“公司A”的唯一路径
MATCH path = (start:Company{name:"Company A"})-[r:Sell*]->(end:Company{name:"Company A"})
RETURN path
它返回五个路径
╒══════════════════════════════════════════════════════════════════════╕
│"path" │
╞══════════════════════════════════════════════════════════════════════╡
│[{"name":"Company A"},{"contract":"TA1805"},{"name":"Company D"},{"nam│
│e":"Company D"},{"contract":"TA1806"},{"name":"Company A"}] │
├──────────────────────────────────────────────────────────────────────┤
│[{"name":"Company A"},{"contract":"TA1805"},{"name":"Company D"},{"nam│
│e":"Company D"},{"contract":"TA1806"},{"name":"Company A"},{"name":"Co│
│mpany A"},{"contract":"TA1801"},{"name":"Company B"},{"name":"Company │
│B"},{"contract":"TA1802"},{"name":"Company C"},{"name":"Company C"},{"│
│contract":"TA1803"},{"name":"Company A"}] │
├──────────────────────────────────────────────────────────────────────┤
│[{"name":"Company A"},{"contract":"TA1801"},{"name":"Company B"},{"nam│
│e":"Company B"},{"contract":"TA1802"},{"name":"Company C"},{"name":"Co│
│mpany C"},{"contract":"TA1803"},{"name":"Company A"}] │
├──────────────────────────────────────────────────────────────────────┤
│[{"name":"Company A"},{"contract":"TA1801"},{"name":"Company B"},{"nam│
│e":"Company B"},{"contract":"TA1802"},{"name":"Company C"},{"name":"Co│
│mpany C"},{"contract":"TA1803"},{"name":"Company A"},{"name":"Company │
│A"},{"contract":"TA1805"},{"name":"Company D"},{"name":"Company D"},{"│
│contract":"TA1806"},{"name":"Company A"}] │
├──────────────────────────────────────────────────────────────────────┤
│[{"name":"Company A"},{"contract":"TA1801"},{"name":"Company B"},{"nam│
│e":"Company B"},{"contract":"TA1802"},{"name":"Company C"},{"name":"Co│
│mpany C"},{"contract":"TA1804"},{"name":"Company D"},{"name":"Company │
│D"},{"contract":"TA1806"},{"name":"Company A"}] │
└──────────────────────────────────────────────────────────────────────┘
但是您看到的与TA1806,TA1801,TA1802的合同的关系出售不止一次。 一个具体的例子是TA1806出现在路线1,2,4和5。TA1801出现在路线2,3,4,5
我希望路径只包含与最短路径的唯一关系(起初我想要最长的路径,但似乎复杂度大大提高了)
╒══════════════════════════════════════════════════════════════════════╕
│"path" │
╞══════════════════════════════════════════════════════════════════════╡
│[{"name":"Company A"},{"contract":"TA1805"},{"name":"Company D"},{"nam│
│e":"Company D"},{"contract":"TA1806"},{"name":"Company A"}] │
├──────────────────────────────────────────────────────────────────────┤┤
│[{"name":"Company A"},{"contract":"TA1801"},{"name":"Company B"},{"nam│
│e":"Company B"},{"contract":"TA1802"},{"name":"Company C"},{"name":"Co│
│mpany C"},{"contract":"TA1803"},{"name":"Company A"}] │
├──────────────────────────────────────────────────────────────────────┤
答案 0 :(得分:5)
对于最长的路径,您可以按长度排序并采取最长的路径,但是APOC可以帮助进行重复检查(起始节点除外,因为您需要电路)
MATCH path = (start:Company{name:"Company A"})-[r:Sell*]->(end:Company{name:"Company A"})
WHERE NOT apoc.coll.containsDuplicates(tail(nodes(path)))
WITH path
ORDER BY length(path) DESC
LIMIT 1
RETURN path
不重复路径中的节点的纯密码方法相当难看:
MATCH path = (start:Company{name:"Company A"})-[r:Sell*]->(end:Company{name:"Company A"})
WHERE all(node in tail(nodes(path)) WHERE single(x in tail(nodes(path)) WHERE x = node))
WITH path
ORDER BY length(path) DESC
LIMIT 1
RETURN path