给定节点最近的节点,为关系类型动态分配权重 [英] Nearest nodes to a give node, assigning dynamically weight to relationship types

查看:106
本文介绍了给定节点最近的节点,为关系类型动态分配权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在图中找到到给定节点最近"的N个节点,这意味着沿着给定节点的路径的关系权重最小的节点. 使用纯Cypher专用解决方案可以做到吗?我当时在寻找路径函数,但是找不到表达我的查询的可行方法.

此外,是否可以根据查询的类型/标签在查询时为关系分配默认权重(或者以其他方式将关系类型映射到权重)?这个想法是用不同的权重进行试验,而不必为每个关系更改属性. 否则,我将不得不将权重属性的值更改为每个关系,并在每次查询之前将其重做一次,这非常耗时(我的图具有大约10M个关系). 同样,纯Cypher解决方案将是最佳选择,或者请向我指出正确的方向.

解决方案

请使用可变长度Cypher查询从单个节点中查找最近的节点.

MATCH (n:Start { id: 0 }),
  (n)-[:CONNECTED*0..2]-(x)
RETURN x

请注意,语法[CONNECTED*0..2]是一个范围参数,用于指定距给定节点的最小和最大关系距离,关系类型为CONNECTED.

您可以将此关系类型交换为其他类型.

在这种情况下,您想从起始节点到周围节点进行可变遍历,但通过停止条件将其限制为阈值,这会有点困难.对于这类事情,熟悉Neo4j的空间插件非常有用.可以在此博客文章中找到学习Neo4j空间的良好起点: https://github.com/neo4j-contrib/spatial

I need to find the N nodes "nearest" to a given node in a graph, meaning the ones with least combined weight of relationships along the path from given node. Is is possible to do so with a pure Cypher only solution? I was looking about path functions but couldn't find a viable way to express my query.

Moreover, is it possible to assign a default weight to a relationship at query time, according to its type/label (or somehow else map the relationship type to the weight)? The idea is to experiment with different weights without having to change a property for every relationship. Otherwise I would have to change the weight property's value to each relationship and re-do it to before each query, which is very time-consuming (my graph has around 10M relationships). Again, a pure Cypher solution would be the best, or please point me in the right direction.

解决方案

Please use variable length Cypher queries to find the nearest nodes from a single node.

MATCH (n:Start { id: 0 }),
  (n)-[:CONNECTED*0..2]-(x)
RETURN x

Note that the syntax [CONNECTED*0..2] is a range parameter specifying the min and max relationship distance from a given node, with relationship type CONNECTED.

You can swap this relationship type for other types.

In the case you wanted to traverse variably from the start node to surrounding nodes but constrain via a stop criteria to a threshold, that is a bit more difficult. For these kinds of things it is useful to get acquainted with Neo4j's spatial plugin. A good starting point to learn more about Neo4j spatial can be found in this blog post: http://neo4j.com/blog/neo4j-spatial-part1-finding-things-close-to-other-things

The post is a little outdated but if you do some Google searching you can find more updated materials.

GitHub repository: https://github.com/neo4j-contrib/spatial

这篇关于给定节点最近的节点,为关系类型动态分配权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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