有些人帮助理解Cypher

时间:2015-10-14 13:12:30

标签: neo4j cypher

我正在编写一个密码查询,将信息合并到我的网站的一个结果中。当我把它们放在一个Cypher组中时,所有结果都是5:

MATCH (p:Person{username:"wkolcz"})
MATCH(p)-[]->(um:Meetup{status:"Accepted"})
WHERE toInt(um.meet_date) >= 1444827367
MATCH(p)-[]->(pm:Meetup{status:"Accepted"})
WHERE toInt(pm.meet_date) <= 1444827367
MATCH (p)-[:MEMBER_OF]->(g:Group)
RETURN count(um) as upcoming, count(pm) as past_meetups, count(g) as group_total

但是当我将它们分解为单个查询并输出结果时,它们是正确的。我错过了什么?谢谢!

MATCH (p:Person{username:"wkolcz"})
MATCH(p)-[]->(pm:Meetup{status:"Accepted"})
WHERE toInt(pm.meet_date) <= 1444827367
RETURN count(pm) as past_meetups

结果为1(正确)

MATCH (p:Person{username:"wkolcz"})
MATCH(p)-[]->(um:Meetup{status:"Accepted"})
WHERE toInt(um.meet_date) >= 1444827367
RETURN count(um) as upcoming

结果为1(正确)

MATCH (p:Person{username:"wkolcz"})
MATCH (p)-[:MEMBER_OF]->(g:Group)
RETURN count(g) as group_total

结果是5(正确)

2 个答案:

答案 0 :(得分:1)

所以问题是你所有的MATCH一起形成了一个大的子图模式匹配,Neo4j匹配了它可以找到的那些匹配的所有排列。可能有一个更好的解决方案,但有一种方法可以解决这个问题,一次只进行一次匹配并使用WITH传递结果:

MATCH (p:Person {username:"wkolcz"})
OPTIONAL MATCH (p)-[]->(um:Meetup {status:"Accepted"})
WHERE toInt(um.meet_date) >= 1444827367
WITH p, count(um) as upcoming

OPTIONAL MATCH (p)-[]->(pm:Meetup {status:"Accepted"})
WHERE toInt(pm.meet_date) <= 1444827367
WITH p, upcoming, count(pm) as past_meetups

OPTIONAL MATCH (p)-[:MEMBER_OF]->(g:Group)
RETURN upcoming, past_meetups, count(g) as group_total

答案 1 :(得分:0)

我认为使用OPTIONAL MATCH可能会有更多的运气:

MATCH (p:Person {username:"wkolcz"})
OPTIONAL MATCH (p)-[]->(um:Meetup {status:"Accepted"})
WHERE toInt(um.meet_date) >= 1444827367
OPTIONAL MATCH (p)-[]->(pm:Meetup {status:"Accepted"})
WHERE toInt(pm.meet_date) <= 1444827367
OPTIONAL MATCH (p)-[:MEMBER_OF]->(g:Group)
RETURN count(um) as upcoming, count(pm) as past_meetups, count(g) as group_total

这也应该为您提供count()为零的条目,MATCH不允许这样做。