简体   繁体   English

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

[英]Neo4j Query - Find all nodes which satisfy a property condition and have relationship

I have a query which runs successfully: 我有一个成功运行的查询:

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;

I need to further filter this query. 我需要进一步过滤此查询。

I need to related r nodes with max(r.date) for each v , and find out how many of those nodes have a relationship with another node of type D 我需要r nodes with max(r.date) for each v关联r nodes with max(r.date) for each v ,并找出其中有多少个节点与另一个D类型的节点有关系

I'm trying this query, it throws a syntax error 我正在尝试此查询,它引发语法错误

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;

I also tried many other combinations, but all throw some syntax error. 我还尝试了许多其他组合,但是都抛出了一些语法错误。 All help is appreciated. 感谢所有帮助。

The main issue is that query parser cannot now if filter(x in rs where x.date=date)[0] really is a node, so this is not allowed in the syntax. 主要问题是查询解析器现在无法确定filter(x in rs where x.date=date)[0]确实是一个节点,因此语法中不允许这样做。 Fortunately, it's possible to work around this at the cost of some verbosity: 幸运的是,有可能以一些冗长的代价解决此问题:

  • Use another WITH clause to introduce an alias ( n ) to the node variable. 使用另一个WITH子句为节点变量引入别名( n )。 That will allow you to use the WHERE <pattern> syntax. 这将允许您使用WHERE <pattern>语法。
  • Also, you cannot introduce new variables in the WHERE clause, so just use (:D) instead of (d:D) . 同样,您不能在WHERE子句中引入新变量,因此只需使用(:D)而不是(d:D)

So the query will look like this (obviously, I have not tested it): 因此查询将如下所示(显然,我尚未对其进行测试):

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM