Neo4j如何匹配子路径

时间:2018-08-15 03:14:09

标签: neo4j

假设我在图中有2棵树:

-fPIC

是否有任何方法可以比较(:TreeA)-->({caption:'b1'})-->({caption:'c1'})-->({caption:'d1'}) +->({caption:'b2'})-->({caption:'c2'}) +->({caption:'b3'}) +->({caption:'b4'})-->({caption:'c4'})-->({caption:'d4'}) | +->({caption:'d5'}) +->({caption:'c6'}) +->({caption:'c7'})-->({caption:'d7'}) (:TreeB)-->({caption:'b4'})-->({caption:'c4'}) TreeB的子路径吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

同构问题很棘手,但是由于我们正在使用树木,因此可以作弊。

如果我们可以匹配从根到末端节点的所有路径,并通过提取并加入节点标题来生成这些路径的文本表示,并且我们对:TreeA和:TreeB都这样做,那么我们可以检查是否:TreeB的所有路径表示形式,它们出现在(至少是):TreeA的路径表示形式之一。

// first get text representation of paths from :TreeA
MATCH p = (:TreeA)-[*]->(end)
WHERE NOT (end)-->()
WITH [node in tail(nodes(p)) | node.caption] as captionPath
WITH reduce(path='', caption in captionPath | path + ',' + caption) as pathText
WITH collect(pathText) as aPaths

// then get text representation of paths from :TreeB
MATCH p = (:TreeB)-[*]->(end)
WHERE NOT (end)-->()
WITH aPaths, [node in tail(nodes(p)) | node.caption] as captionPath
WITH aPaths, reduce(path='', caption in captionPath | path + ',' + caption) as pathText
WITH aPaths, collect(pathText) as bPaths

// ensure all text representations of :TreeB occur in at least one of the paths from :TreeA
RETURN all(path in bPaths WHERE any(aPath in aPaths WHERE aPath STARTS WITH path))

请注意,如果您使用的是APOC程序,则可以替换使用reduce函数:

reduce(path='', caption in captionPath | path + ',' + caption) as pathText

使用join()函数:

apoc.text.join(captionPath, ',') as pathText