Neo4j查询 - 查找满足属性条件并具有关系的所有节点

时间:2017-10-11 16:22:33

标签: neo4j cypher

我有一个成功运行的查询:

match (n:A {tag_no:"N2203"})<-[:rel_a]-(v:B)-[:rel_b]->(r)<-[:rel_c]-(n) WHERE (v.invoice_date>="2012-08-01" AND v.invoice_date<"2016-02-01") with v, collect(r) as rs where all (x in rs where x.date<"2016-08-01") return count(v) as count;

我需要进一步过滤此查询。

我需要关联r nodes with max(r.date) for each v,并找出这些节点中有多少与D类型的另一个节点有关系

我正在尝试此查询,它会引发语法错误

match (n:A {tag_no:"N2203"})<-[:rel_a]-(v:B)-[:rel_b]->(r)<-[:rel_c]-(n) WHERE (v.invoice_date>="2012-08-01" AND v.invoice_date<"2016-02-01") with v, max(r.date) as date collect(r) as rs where (all (x in rs where x.date<"2016-08-01") AND filter(x in rs where x.date=date)[0]<-[:rel_c]-(d:D)) return count(v) as count;

我还尝试了很多其他组合,但都抛出一些语法错误。感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

主要问题是,如果filter(x in rs where x.date=date)[0]确实是一个节点,则查询解析器现在不能,因此语法中不允许这样做。幸运的是,有可能以一些冗长的代价来解决这个问题:

  • 使用另一个WITH子句向节点变量引入别名(n)。这将允许您使用WHERE <pattern>语法。
  • 此外,您无法在WHERE子句中引入新变量,因此只需使用(:D)代替(d:D)

所以查询看起来像这样(显然,我还没有测试过):

MATCH (n:A {tag_no:"N2203"})<-[:rel_a]-(v:B)-[:rel_b]->(r)<-[:rel_c]-(n)
WHERE v.invoice_date>="2012-08-01"
  AND v.invoice_date<"2016-02-01"
WITH
  v,
  max(r.date) AS date,
  collect(r) AS rs
WHERE all(x IN rs WHERE x.date<"2016-08-01")
WITH filter(x in rs where x.date=date)[0] AS n, v
WHERE (n)<-[:rel_c]-(:D)
RETURN count(v) AS count;