gpt4 book ai didi

neo4j - Cypher:限制选择节点的返回链接数

转载 作者:行者123 更新时间:2023-12-05 03:12:24 31 4
gpt4 key购买 nike

我在一个看似简单的查询上失败了。

算法:

  1. 按某个值(desc)对节点进行排序:

    MATCH (n) WHERE has(n.II_VAL) WITH n, n.II_VAL as ShInflInd order by ShInflInd desc
  2. 对于每个节点,返回它的直接邻居(按第二个值排序):

    MATCH (n)-[r]->(m) with n, m, m.VALUE as SubsOpRev order by SubsOpRev desc

挑战是返回前 100 个 n 个节点,并且每个节点只有 10 个关系 r(如果可能的话在一行中)。

编辑:

很抱歉我的表述不明确以至于人们误解了我。

详细来说,以下查询为我提供了排名靠前的节点:

MATCH (n) WHERE HAS(n.II_VAL)
WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC
RETURN n.NAME LIMIT 100;

+--------------------------------------+
| n.NAME |
+--------------------------------------+
| "PUBLIC" |
| "BARCLAYS PLC" |

现在我可以将子查询添加到此添加链接:

MATCH (n) WHERE HAS(n.II_VAL)
WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC LIMIT 100
MATCH (n)-[r]->(m) WHERE HAS(m.VALUE)
WITH r, n, m, m.VALUE AS SubsOpRev
RETURN n.NAME, r.WEIGHT_MERGED, m.NAME, SubsOpRev LIMIT 10;

+----------------------------------------------------------------------------------------+
| n.NAME | r.WEIGHT_MERGED | m.NAME | SubsOpRev |
+----------------------------------------------------------------------------------------+
| "PUBLIC" | 0.66 | "VBS MUTUAL BANK" | 2630 |
| "PUBLIC" | 0.2923 | "STRATCORP LIMITED" | 10842 |

现在我想要的是,在返回“PUBLIC”的 10 个链接(可能按 r.WEIGHT_MERGED 或 SubsOpRev 排序)后,查询返回第二个节点(“BARCLAYS PLC”)及其 10 个链接等。

我试过:

MATCH (n) WHERE HAS(n.II_VAL)
WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC
MATCH (n)-[r]->(m) WHERE HAS(m.VALUE)
WITH r, n, m, m.VALUE AS SubsOpRev
RETURN collect([n.NAME, r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..10];

导致:

+------------------------------------------------------------------------------------------------------------------------------------------+
| collect([n.NAME, r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..3] |
+------------------------------------------------------------------------------------------------------------------------------------------+
| [["PUBLIC",0.66,"VBS MUTUAL BANK",2630],["PUBLIC",0.2923,"STRATCORP LIMITED",10842], ...

这意味着我仍然坚持使用“PUBLIC”。

稍微修改查询会使事情变得更糟,因为它返回完全不同的数据:

MATCH (n) WHERE HAS(n.II_VAL)
WITH n, n.`II_VAL` AS ShInflInd ORDER BY ShInflInd DESC
MATCH (n)-[r]->(m) WHERE HAS(m.VALUE)
WITH r, n, m, m.VALUE AS SubsOpRev
RETURN n.NAME, collect([r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..10] LIMIT 3;

+------------------------------------------------------------------------------+
| n.NAME | collect([r.WEIGHT_MERGED, m.NAME, SubsOpRev])[0..10] |
+------------------------------------------------------------------------------+
| "RS-INVEST AS" | [[0.5,"VERUCO EIENDOM AS",100]] |
| "DBM" | [[0.1435,"CHELYABINSKOBLGAZ",6752]] |

理想情况下,查询应该产生类似于

| [["PUBLIC",0.66,"VBS MUTUAL BANK",2630],["PUBLIC",0.2923,"STRATCORP LIMITED",10842], ... |
| [["BARCLAYS PLC",x,"XYZ",y], ... |

最佳答案

您只需要限制然后继续您的查询。为了提供可重现的示例,让我们切换到 Neo4j 附带的电影数据集。假设您想要获取数据库中最老的 3 部电影,然后是每部电影中最老的 2 位 Actor 。

MATCH (m:Movie)
WITH m ORDER BY m.released LIMIT 3
MATCH (p:Person)-[:ACTED_IN]->(m)
WITH m, p ORDER BY p.born
WITH m, COLLECT(p.name)[0..2] AS oldest
RETURN m.title, oldest;

这会产生:

   | m.title                         | oldest                              
---+---------------------------------+--------------------------------------
1 | Something's Gotta Give | ['Jack Nicholson', 'Diane Keaton']
2 | Top Gun | ['Tom Skerritt', 'Kelly McGillis']
3 | One Flew Over the Cuckoo's Nest | ['Jack Nicholson', 'Danny DeVito']

所以你会想要这样的东西:

MATCH (n) WHERE HAS(n.II_VAL)
WITH n ORDER BY n.II_VAL DESC LIMIT 100
MATCH (n)-[r]->(m) WHERE HAS(m.VALUE)
WITH n, r, m ORDER BY m.VALUE DESC
RETURN n.NAME, COLLECT([r.WEIGHT_MERGED, m.NAME, m.VALUE])[0..10];

关于neo4j - Cypher:限制选择节点的返回链接数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33914217/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com