简体   繁体   English

Neo4j / Cypher-查找具有2个以上链接的已连接节点

[英]Neo4j/Cypher - find connected nodes that have more than 2 links

I have a graph as follows (removed any labels or link directions for simplicity) 我有一个如下图(为简单起见,删除了所有标签或链接方向)

简单的示例图

I would like to start at node (c) and find only those nodes that have more than 2 adjacent edges, and the paths to them from (c). 我想从节点(c)开始,仅查找具有两个以上相邻边的那些节点,以及从(c)到它们的路径。

In the example above, node (b) has 3 adjacent edges (ba, bc, bg) and node (e) has 4 adjacent edges (ed, ef, eh, eh), so I would like to return the paths to just (b) and (e). 在上面的示例中,节点(b)具有3个相邻边(ba,bc,bg),节点(e)具有4个相邻边(ed,ef,eh,eh),因此我想将路径返回到just( b)和(e)。

I also do not want to return the path to (a), (f), (h), (g) or (j) - I want to stop the traversal when the count is satisfied. 我也不想返回路径(一),(六),(八),(g)或(J) -我想停止遍历当计数满足。

I've tried the following: 我尝试了以下方法:

START n=node(c)
MATCH (n)-[r*]-(m)-[rx]-(o)
WITH m AS m, n AS n, r AS r, count(rx) as cnt
WHERE cnt > 2
RETURN n, r, m, cnt;

... but it returns paths to a, g, h, f and j in addition to b and e. ...但是除了b和e之外,它还返回到a,g,h,f和j的路径。 It is also very costly for a big graph. 对于大图来说,这也是非常昂贵的。

Very grateful for any help. 非常感谢您的帮助。

EDIT: 编辑:

The example image I provided oversimplifies my data, so the initial suggestion doesn't work (see http://console.neo4j.org/?id=d6feml ) so a new image example provided below. 我提供的示例图像过度简化了我的数据,因此最初的建议不起作用(请参阅http://console.neo4j.org/?id=d6feml ),因此下面提供了一个新的图像示例。

I want : The paths to e an b only - as before. 我想要 :到e a b的路径-和以前一样。

I don't want : to return the path to h. 我不想 :返回路径到h。

在此处输入图片说明

Thanks again neo4jers... 再次感谢neo4jers ...

Interesting one, I've put it into Neo4j console using http://console.neo4j.org/r/qc7log . 有趣的是,我使用http://console.neo4j.org/r/qc7log将其放入Neo4j控制台。

The Cypher statement you're looking for is: 您要查找的Cypher语句是:

START n=node(2) // node c has node id = 2
MATCH p=(n)-[KNOWS*]-(m),(m)-[:KNOWS]-(x)
WITH p, count(x) AS count
WHERE count>2
RETURN p

The trick here is specify the path in the MATCH in two parts. 这里的技巧是分两部分指定MATCH中的路径。 The first part is then used for the aggregation using WITH and for the RETURN . 然后,将第一部分用于使用WITHRETURN进行聚合。

Based on Stefans suggestion to look at the traversal framework, I spent a while trying to figure it out in python (neo4j-rest-client). 基于Stefans的建议来研究遍历框架,我花了一段时间尝试在python(neo4j-rest-client)中弄清楚它。 The following snippet now works: 现在可以使用以下代码段:

# Retrieve the node at the start of the traveral
seed_node = gdb.nodes(node_id)

# Establish what the traversal should look like
simpleTraversalTemplate = traversals.TraversalDescription().relationships('adjacent')

# Set some un-chainable attributes - work breadth first and set max depth of traversal
simpleTraversalTemplate.breadthFirst()

# This is how far you think it will go before meeting a junction. 100 is high.
simpleTraversalTemplate.max_depth(100)

# Set up a prune evaluator. This by itself (without the filter evaluator)
# returns all traversals up to the point where the link count is > 2. This includes
# the traversals to the nodes before the >2 link_count node. 
simpleTraversalTemplate.prune(value={'body':"parseInt(position.endNode().getProperty('link_count')) > 2;",
                                     'language':'javascript'})

# So, a filter is used with the prune to only return those traversals where the end node link_count 
# is >2. With only this (ie without prune), the traversal would continue and just keep returning
# those nodes with link_count > 2. 
simpleTraversalTemplate.filter(value={'body':"parseInt(position.endNode().getProperty('link_count')) > 2;",
                                     'language':'javascript'})

# Now run the traverser based on the seed node
trav = simpleTraversalTemplate.traverse(seed_node)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Neo4j Cypher Query:查找连接到一个节点且具有 3 个以上其他关系的所有节点 - Neo4j Cypher Query: Finding all nodes, that are connected to a node, that has more than 3 other relationships Neo4j Cypher查询查找未连接的节点太慢 - Neo4j Cypher query to find nodes that are not connected too slow 标签,属性或节点? Cypher支架/ Neo4j的 - Labels, properties, or nodes? Cypher/Neo4j 使用cypherquery在neo4j中仅查找具有多个传入关系的节点 - Finding only nodes that have more than one incoming relationship in neo4j using cypherquery Neo4j Cypher:在一组匹配的节点之间查找公共节点 - Neo4j Cypher: Find common nodes between a set of matched nodes Neo4j:如何查找所有节点:连接到具有特定属性的0个节点或具有属性集的所有节点 - Neo4j: How to find all nodes that: connected to either 0 nodes with specific property, or all nodes with property set Neo4j - 使用Cypher从节点开始并将Traverse图表移动到指定的深度并查找节点和关系 - Neo4j - Using Cypher start at a node and Traverse graph to a specified depth and find nodes and relationships 如何在没有无限循环的情况下找到 Neo4j 中有向图中连接到某些节点的所有节点? - How to find all nodes connected to certain nodes in a directed graph in Neo4j without infinite loops? 为什么此查询的计数超出预期? (密码/ neo4j)? - Why is this query increasing count by more than expected? (cypher/neo4j)? 使用Cypher在Neo4j中创建多个节点 - Creation of multiple nodes in Neo4j using Cypher
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM