2

我先取n邻居:(在此示例中,n = 6)

我有一个加权图,所以我也按重量排序结果:

START start_node=node(1859988)
MATCH start_node-[rel]-(neighbor)
RETURN DISTINCT neighbor,
rel.weight AS weight ORDER BY proximity DESC LIMIT 6;

我想获取一个完整的子图,包括第二个邻居(前六个孩子的第一个邻居)。

我试过 smtg 喜欢:

START start_node=node(1859988)
MATCH start_node-[rel]-(neighbor)
FOREACH (neighbor | MATCH neighbor-[rel2]-(neighbor2) )
RETURN DISTINCT neighbor1, neighbor2, rel.proximity AS proximity ORDER BY proximity DESC LIMIT 6, rel2.proximity AS proximity ORDER BY proximity DESC LIMIT 6;

语法仍然错误,但我也不确定输出:我想要一个元组表,父、子和权重:[node_A - node_B - weight]

我想看看它是执行一个查询还是六个查询更好。有人可以帮助澄清如何迭代查询(FOREACH)并格式化输出吗?

谢谢你!

4

2 回答 2

1

首先,您应该避免使用 START,因为它(希望)最终会消失。

因此,要获得一个邻域,您可以使用可变长度路径来获取远离节点的所有路径

MATCH path=start_node-[rel*1..3]-(neighbor)
WHERE ID(start_node) = 1859988
RETURN path, nodes(path) AS nodes, EXTRACT(rel IN rels(path) | rel.weight) AS weights;

然后您可以选择路径/节点并将它们与您选择的语言组合在内存中。

编辑:

也看看这个 SO Question: Fetch a tree with Neo4j

它显示了如何将输出作为每个关系的一组开始/结束节点,这在许多情况下可能会更好。

于 2015-07-01T22:13:18.437 回答
1

好的,我想我明白了。这是基于您的评论的另一种尝试:

MATCH (start_node)-[rel]-(neighbor)
WHERE ID(start_node) IN {source_ids}
WITH
  neighbor, rel
ORDER BY rel.proximity
WITH
  collect({neighbor: neighbor, rel: rel})[0..6] AS neighbors_and_rels
UNWIND neighbors_and_rels AS neighbor_and_rel
WITH
  neighbor_and_rel.neighbor AS neighbor,
  neighbor_and_rel.rel AS rel
MATCH neighbor-[rel2]-(neighbor2)
WITH
  neighbor,
  rel,
  neighbor2,
  rel2
ORDER BY rel.proximity
WITH
  neighbor,
  rel,
  collect([neighbor2, rel2])[0..6] AS neighbors_and_rels2
UNWIND neighbors_and_rels2 AS neighbor_and_rel2
RETURN
  neighbor,
  rel,
  neighbor_and_rel2[0] AS neighbor2,
  neighbor_and_rel2[1] AS rel2

它有点长,但希望它至少能给你一个想法

于 2015-07-02T17:16:51.113 回答