Neo4j密码查询,计数节点和双向关系

时间:2019-03-26 13:17:55

标签: neo4j nodes relationship counting

我正在尝试在具有两种联系的节点上运行密码查询,并且也希望对每个联系进行计数。

示例:

Nodes: store + customer 
Relationships: sold + bought

在理想情况下,我希望看到将商店和客户联系在一起的两种关系。 但是,在某些情况下,只有客户报告他们从商店购买了商品,反之亦然,只有客户报告了他们向商店出售了商品,但是客户尚未验证该销售。

我想返回以下内容:

  • 商店名称
  • 买卖关系的数量(商店<->客户)
  • 仅销售关系的数量(商店->客户)
  • 仅购买的关系数(商店<—客户)

***说明:

例如,它将有10个商店,每个商店有7-10个客户,是的,在Neo中,它将是两个不同的弧形连接:

store-customer r/ships

目标是查看每家商店的不同报告惯例,因为有些商店可能会说他们在没有的时候就卖了,而顾客可能会说他们买了商品。这些情况中的每种情况多久发生一次?

2 个答案:

答案 0 :(得分:0)

您可以使用类似的

match (n:Store)<-[r:bought]-(n1:Customer)<-[r1:sold]-(n) with count(distinct r) as verified 
match (n:Store)<-[r:bought]-(n1:Customer) with verified, count(distinct r) - verified as boughtOnly 
match (n:Store)-[r:sold]->(n1:Customer) with verified, boughtOnly, count(distinct r) - verified as soldOnly 
return verified, soldOnly, boughtOnly

请参见虚拟数据库here进行测试

编辑 使用where子句更新查询并返回store信息

match (n:Store) where n.state = 'MA' and n.city = 'Boston' with n as Store
match (Store)<-[r:bought]-(n1:Customer)<-[r1:sold]-(n) with Store, count(distinct r) as verified 
match (Store)<-[r:bought]-(n1:Customer) with Store, verified, count(distinct r) - verified as boughtOnly 
match (Store)-[r:sold]->(n1:Customer) with Store, verified, boughtOnly, count(distinct r) - verified as soldOnly 
return verified, soldOnly, boughtOnly, Store

答案 1 :(得分:0)

就这么简单:

MATCH (n:Customer)
RETURN 
size((n)-[:BOUGHT|:SOLD]-()) AS bothRels,
size((n)-[:BOUGHT]-()) AS boughtRels,
size((n)-[:SOLD]-()) AS soldRels
╒══════════╤════════════╤══════════╕
│"bothRels"│"boughtRels"│"soldRels"│
╞══════════╪════════════╪══════════╡
│2         │1           │1         │
└──────────┴────────────┴──────────┘