Neo4j密码:结合汇总,排序和限制

时间:2019-04-08 21:26:27

标签: neo4j cypher

Cypher相对较新,我从文档中看不到如何获得所需的东西,这是ORDER BY和LIMIT与聚合的结合,我猜想是在使用COLLECT。

拍摄下图:

CREATE (colombia {name:'Colombia', pop:50})
CREATE (argentina {name:'Argentina', pop:43})
CREATE (peru {name:'Peru', pop:32})
CREATE (chile {name:'Chile', pop:17})
CREATE (ecuador {name:'Ecuador', pop:16})
CREATE (bolivia {name:'Bolivia', pop:11})
CREATE (paraguay {name:'Paraguay', pop:7})
CREATE (uruguay {name:'Uruguay', pop:3.4})
CREATE (venezuela {name:'Venezuela', pop:32})
CREATE
  (colombia)-[:BORDERS {length:586}]->(ecuador),
  (colombia)-[:BORDERS {length:2219}]->(venezuela),
  (colombia)-[:BORDERS {length:1800}]->(peru),
  (ecuador)-[:BORDERS {length:1420}]->(peru),
  (peru)-[:BORDERS {length:171}]->(chile),
  (peru)-[:BORDERS {length:1075}]->(bolivia),
  (chile)-[:BORDERS {length:6691}]->(argentina),
  (argentina)-[:BORDERS {length:942}]->(bolivia),
  (argentina)-[:BORDERS {length:1880}]->(paraguay),
  (argentina)-[:BORDERS {length:541}]->(uruguay),
  (bolivia)-[:BORDERS {length:750}]->(paraguay)

为了说明我需要考虑的查询,

MATCH (a)-[ab]-(b)-[bc]-(c) 
WHERE a.name = 'Bolivia'
WITH a, b, c, bc
ORDER BY b.pop DESC, bc.length DESC
RETURN a.name, b.name, b.pop, c.name, bc.length

╒═════════╤═══════════╤═══════╤═══════════╤═══════════╕
│"a.name" │"b.name"   │"b.pop"│"c.name"   │"bc.length"│
╞═════════╪═══════════╪═══════╪═══════════╪═══════════╡
│"Bolivia"│"Argentina"│43     │"Chile"    │6691       │
├─────────┼───────────┼───────┼───────────┼───────────┤
│"Bolivia"│"Argentina"│43     │"Paraguay" │1880       │
├─────────┼───────────┼───────┼───────────┼───────────┤
│"Bolivia"│"Argentina"│43     │"Uruguay"  │541        │
├─────────┼───────────┼───────┼───────────┼───────────┤
│"Bolivia"│"Peru"     │32     │"Colombia" │1800       │
├─────────┼───────────┼───────┼───────────┼───────────┤
│"Bolivia"│"Peru"     │32     │"Ecuador"  │1420       │
├─────────┼───────────┼───────┼───────────┼───────────┤
│"Bolivia"│"Peru"     │32     │"Chile"    │171        │
├─────────┼───────────┼───────┼───────────┼───────────┤
│"Bolivia"│"Paraguay" │7      │"Argentina"│1880       │
└─────────┴───────────┴───────┴───────────┴───────────┘

我需要首先通过 pop 属性将结果限制在前2个匹配的(b)节点上,然后才将结果限制在前2个(c)节点上,并分别用[bc.length] (b)的情况。在此示例中,这意味着行1,2、4和5。

我已经尝试过COLLECT,但是一无所获。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

此查询:

MATCH (a)--(b)-[bc]-(c) 
WHERE a.name = 'Bolivia'
WITH a, b, c, bc
ORDER BY b.pop DESC, bc.length DESC
WITH a, b, COLLECT({c: c, bc: bc}) AS data
LIMIT 2
UNWIND data[..2] AS d
RETURN a.name, b.name, b.pop, d.c.name, d.bc.length

返回:

╒═════════╤═══════════╤═══════╤══════════╤═════════════╕
│"a.name" │"b.name"   │"b.pop"│"d.c.name"│"d.bc.length"│
╞═════════╪═══════════╪═══════╪══════════╪═════════════╡
│"Bolivia"│"Argentina"│43     │"Chile"   │6691         │
├─────────┼───────────┼───────┼──────────┼─────────────┤
│"Bolivia"│"Argentina"│43     │"Paraguay"│1880         │
├─────────┼───────────┼───────┼──────────┼─────────────┤
│"Bolivia"│"Peru"     │32     │"Colombia"│1800         │
├─────────┼───────────┼───────┼──────────┼─────────────┤
│"Bolivia"│"Peru"     │32     │"Ecuador" │1420         │
└─────────┴───────────┴───────┴──────────┴─────────────┘

COLLECTaggregating function,它在同一WITH(或RETURN)子句中使用非聚合项作为“分组关键字”。在此查询中,COLLECT收集cbc值的每个不同对(和有序)对的ab值的有序列表。 LIMIT子句将WITH结果限制为前两个ab对(及其data列表)。