路径中集合节点的Neo4J公共邻居 [英] Neo4J common neighbors of a set nodes in a path

查看:600
本文介绍了路径中集合节点的Neo4J公共邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络由节点组成,关系是数字.可以把它想象成一组城市,而关系就是是否有道路,如果有那么远.

My network consists of nodes and the relationship is a numeric number. Think of it as a set of cities, and the relationship is the whether there is a road and if so how far is it.

我有我的neo4j查询的路径,想知道在以下条件下如何找到该路径的邻居.这些邻居应该与路径中的多个节点相邻. 在下面的图片中,我试图说明我的意思.我的路径看起来像下面的蓝星.我想找到绿色的节点.这些绿色节点连接到路径中的两个或更多节点.我画了一些绿色节点.

I have path from my neo4j query, wonder how I can find the neighbor of this path given the following condition. These neighbors should be neighbor to more than one node in the path. In the following picture, I have tried to illustrate what I mean. My path looks like the blue star below. I would like to find the green nodes. These green nodes, are connected to two or more nodes in the path. I have draw a few of these green nodes.

作为输出,我希望有一条包含蓝色路径和绿色路径的路径.

As an output I would like to have a path that include the blue path as well as the green ones.

编辑

我的原始路径看起来像

My original path looks like

如果我使用@NonameCurious建议的解决方案,我将拥有

If I use the suggested solution by @NonameCurious, I will have

如您所见,结果是一组没有关系的节点.我认为这是因为查询仅返回节点.但是,我希望具有原始路径的那些邻居"之间的连接显示在原始路径的顶部.

As you can see the result is a group of nodes which there is no relationship. I assume it is because the query only returns nodes. However, I would like to have the connection between those "neighbors" with the original path be displayed on top of the original path.

推荐答案

这是怎么回事:

WITH nodes(path) AS nodes
UNWIND nodes AS node
MATCH (a)--(node) WHERE NOT a IN nodes
WITH a, COUNT(DISTINCT node) AS relCounts
WITH a WHERE relCounts > 1
RETURN a

我假设给出了path.

更新:

如果您需要过滤关系,则可以使用以下方式:

If you need to filter relationships, you can use something like this:

WITH nodes(path) AS nodes
UNWIND nodes AS node
MATCH (a)-[r]-(node) WHERE NOT a IN nodes AND r.score > 27
WITH a, COUNT(DISTINCT node) AS relCounts
WITH a WHERE relCounts > 1
RETURN a

第二次更新: 如果您只想以某种方式获取所有新节点以及旧节点的子图,则可以这样做

SECOND UPDATE: If you just want to somehow get a subgraph of all the new nodes along with the old ones, you can do this

WITH nodes(path) AS nodes, path UNWIND nodes AS node MATCH (a)-[r]-(node) 
WHERE NOT a IN nodes AND r.score > 27 
WITH a, COLLECT(DISTINCT node) AS connectedNodes, COLLECT(DISTINCT r) AS connectedRels, path WHERE SIZE(connectedNodes) > 1 
UNWIND connectedNodes AS connectedNode 
UNWIND connectedRels AS connectedRel 
RETURN a, connectedRel, connectedNode, path

这篇关于路径中集合节点的Neo4J公共邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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