Neo4j基于边权重对相邻节点进行排名

lsmd5eda  于 2022-11-05  发布在  其他
关注(0)|答案(2)|浏览(161)

假设我们有一个有 * 个节点(n)* 和 * 条边[e]* 的图,所有的边都有属性,其中至少有一个属性是数值属性,称为 'weight'
现在我想知道每个节点的“最紧密连接”的相邻(兄弟/一跳距离)节点是什么。作为基础,我想根据权重属性降序排列相邻节点。
在Neo4j中,这个查询是什么?(在SQL中,我们可以使用像RANK()这样的函数)
例如,假设我的节点有4个权重为(3,1,8,5)的邻居节点,那么查询应该返回行a)节点的id b)节点的权重c)节点的总排名,所以在这种情况下是(3,4,1,2)。

ds97pgxw

ds97pgxw1#

你的例子对我来说不是很清楚,但如果我理解正确的话,你想要的是这样的东西:

MATCH (n:Person)-[r]-(m)
WITH n, r.weight as w, m
ORDER BY w DESC
WITH collect ({name: m.name, w: w}) as ms, n
RETURN n, ms

其中对于本示例数据:

MERGE (b:Person {name: "Bob" })
MERGE (c:Person {name: "Charles" })
MERGE (a:Person {name: "Anna" })
MERGE (d:Person {name: "Kira" })
MERGE (e:Person {name: "Dan" })
MERGE (f:Person {name: "El" })

MERGE (a)-[:LIKES{weight: 7}]-(b)
MERGE (a)-[:LIKES{weight: 2}]-(c) 
MERGE (a)-[:LIKES{weight: 1}]-(d) 
MERGE (e)-[:LIKES{weight: 8}]-(a)
MERGE (e)-[:LIKES{weight: 3}]-(b) 
MERGE (f)-[:LIKES{weight: 2}]-(a) 
MERGE (d)-[:LIKES{weight: 5}]-(b)

退货:

╒══════════════════╤═══════════════════════════════════════════════════╕
│"n"               │"ms"                                               │
╞══════════════════╪═══════════════════════════════════════════════════╡
│{"name":"Anna"}   │[{"w":8,"name":"Dan"},{"w":7,"name":"Bob"},{"w":2,"│
│                  │name":"Charles"},{"w":2,"name":"El"},{"w":1,"name":│
│                  │"Kira"}]                                           │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Dan"}    │[{"w":8,"name":"Anna"},{"w":3,"name":"Bob"}]       │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Bob"}    │[{"w":7,"name":"Anna"},{"w":5,"name":"Kira"},{"w":3│
│                  │,"name":"Dan"}]                                    │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Kira"}   │[{"w":5,"name":"Bob"},{"w":1,"name":"Anna"}]       │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"Charles"}│[{"w":2,"name":"Anna"}]                            │
├──────────────────┼───────────────────────────────────────────────────┤
│{"name":"El"}     │[{"w":2,"name":"Anna"}]                            │
└──────────────────┴───────────────────────────────────────────────────┘
rkkpypqq

rkkpypqq2#

您可以使用ORDER BY对结果进行排序。

MATCH (n:NodeLabel {nodeid:YourNode})-[rel:RelationshipLabel]-(m:NodeLabel)
RETURN id(m) as NodeId, rel.weight as Weight
ORDER BY Weight DESC

不确定如何返回等级。

相关问题