Neo4j如何匹配子路径
假设图中有两棵树:Neo4j如何匹配子路径,neo4j,Neo4j,假设图中有两棵树: (:TreeA)-->({caption:'b1'})-->({caption:'c1'})-->({caption:'d1'}) +->({caption:'b2'})-->({caption:'c2'}) +->({caption:'b3'}) +->({caption:'b4'})-->({caption:'c4'})-->({caption:'d4'})
(: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
是TreeA
的子路径,有没有办法比较
非常感谢同构问题很棘手,但由于我们使用的是树,所以可能会有点作弊 如果我们可以匹配从根节点到结束节点的所有路径,并通过提取和连接节点标题来生成这些路径的文本表示,并且我们对: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='',captionPath中的标题| path+,'+标题)为pathText
使用join()函数:
apoc.text.join(captionPath,,')作为路径文本