Neo4j:仅返回图中的第一个公共节点并丢弃公共子节点 [英] Neo4j: Return only the first common node in the graph discarding common children

查看:112
本文介绍了Neo4j:仅返回图中的第一个公共节点并丢弃公共子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从蓝色的节点开始,我想将每个蓝色的节点与蓝色的节点进行比较,以寻找它们的第一个公共子节点.(绿色)我下面的当前查询将第一个公共节点返回到所有起始值以及所有的公共子节点节点. (红色:左侧图表)

Starting with nodes in blue I want to compare each to the other in blue looking for their first common child nodes.(in green) My current query below is returning the first common node to all starting values and also all the common child nodes. (in red: left-hand graph)

如何过滤结果,以便仅返回第一个公共节点(绿色),丢弃白色和红色节点.

How can I filter the results so only the first common nodes (in green) are returned discarding the white and red nodes.

我从2-10个值开始,当只需要几个根节点时,当前可以有100多个结果行.我想我需要收集结果,然后对集合进行另一个模式匹配-也许比较寻找第一个公共节点的路径.每个起始集可能有几个根(绿色)节点.

I am starting with 2 - 10 values and currently can have over 100 result rows when only a couple of their root node(s) are required. I think I need to collect the results then do another pattern match on the collection - Maybe compare the paths looking for the first common node. There may be several root (green) nodes for each starting set.

感谢您的帮助!

MATCH (val0:v {value:"532"} )-[r*0..50]->(x:n)  WITH x
MATCH (val1:v {value:"234"} )-[r*0..50]->(x:n)  WITH x
MATCH (val2:v {value:"678"} )-[r*0..50]->(x:n) 

RETURN DISTINCT con

推荐答案

我认为您有正确的主意,收集结果,然后进行其他匹配以查找保留哪些结果.我认为 APOC程序在这里可以提供帮助,因为其

I think you've got the right idea, collecting the results and then doing some other match to find which of those results to keep. I think APOC Procedures can help here, as its path finding procs have ways to stop when reaching certain nodes (the terminatorNodes config parameter).

您还将希望沿途收集结果,这样就不会增加需要完成的工作量.例如,第一次匹配中x的行数将乘以第二次匹配的次数.如果我们沿途将结果收集到列表中,则每个扩展仅执行一次,而不是每行一次乘法(Cypher操作每行执行一次).我们可以使用列表交集(来自APOC)来获得沿途相同的节点.

You'll also want to collect your results along the way so you don't multiply the amount of work that needs to be done. For example, the number of rows for x from your first match will multiply the number of times you perform the second match. If we collect results into lists along the way, then we only execute each expansion once instead of multiplicatively per row (Cypher operations execute per row). We can use list intersection (from APOC) to get the nodes in common along the way.

然后,我们可以使用路径查找器proc从终止节点过滤器中查找任何起始节点的路径,这样,当我们到达这些常见节点结果之一(即您的绿色节点)时,探索的每条路径都会停止想要).

Then we can use the path finder procs to find a path from any of your starting nodes, with a termination node filter so each path explored will stop when we reach one of those common node results (which will be the green nodes you want).

以下是一个可能对您有用的示例:

Here's an example that may work for you:

MATCH (val0:v {value:"532"} )-[*0..50]->(x:n)
WITH collect(distinct x) as results
MATCH (val1:v {value:"234"} )-[*0..50]->(x:n)  
WITH apoc.coll.intersection(results, collect(distinct x)) as results
MATCH (val2:v {value:"678"} )-[*0..50]->(x:n) 
WITH apoc.coll.intersection(results, collect(distinct x)) as results
MATCH (start:v {value:"532"} )
CALL apoc.path.subgraphNodes(start, {terminatorNodes:results}) YIELD node as firstCommon
RETURN firstCommon

编辑

就非APOC方法而言,类似这样的方法可能会起作用,替换apoc.path.subgraphNodes()调用:

As far as a non-APOC approach, something like this might work, replacing the apoc.path.subgraphNodes() call:

...
MATCH p=(start:Root {id:532} )-[*0..50]->(x)
WHERE x in results and single(node in nodes(p) where node in results)
RETURN distinct x

这篇关于Neo4j:仅返回图中的第一个公共节点并丢弃公共子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆