简体   繁体   English

Neo4J Cypher排除反向路径

[英]Neo4J Cypher Exclude Reverse Path

I have a simple graph where nodes are connected to other nodes eg 我有一个简单的图,其中节点连接到其他节点,例如

Person -[Shops_At]-> Shop 人-[Shops_At]->商店

I would like find the number of customers each Shop has, I can do this by matching 我想找到每个商店的客户数量,我可以通过匹配
Shop <-[Shops_At]- Person -[Shops_At]-> Shop 店铺<-[Shops_At]-人-[Shops_At]->店铺
And counting the number of Person nodes. 并计算Person节点的数量。 I can exclude the case where the Shops are the same. 我可以排除商店相同的情况。

In Cypher how do I stop getting both directions of the path. 在Cypher中,我如何停止获取路径的两个方向。 eg (with id's) 例如(带有ID)

194 <-[Shops_At]- 18 -[Shops_At]-> 200 194 <-[Shops_At]-18-[Shops_At]-> 200
200 <-[Shops_At]- 18 -[Shops_At]-> 194 200 <-[Shops_At]-18-[Shops_At]-> 194

I only want each path once. 我只希望每个路径一次。

Update: So The reason I want to do the above query but only get each path once is that I ultimately want to generate a graph where the shops are connected together by an edge that has the weight of the number of shared customers. 更新:因此,我想执行上述查询但只获取每条路径一次的原因是,我最终想要生成一个图形,在该图形中,商店通过具有共享客户数量权重的边连接在一起。

So for the above. 所以以上。

200 -[Shares Customers, 18]- 194 200-[Shares客户,18]-194

I don't want the reverse path in the new graph. 我不希望新图中的反向路径。

I have determined a way of doing what I wanted. 我已经确定了一种做我想要的方法。
In the cypher query I can insist on the ID of one shop being smaller than the other 在密码查询中,我可以坚持认为一个商店的ID小于另一个商店的ID

match (n:`shop`)--(p:`person`)--(m:`shop`) WHERE id(n) < id(m) return n,count(p),m 匹配(n:`shop`)-(p:`person`)-(m:`shop`)其中id(n)<id(m)返回n,count(p),m

You can do this with a simpler solution : 您可以使用更简单的解决方案:

First match the shops, you'll not have duplicates and count the customers they have: 首先匹配商店,您将没有重复的商品并计算他们拥有的顾客:

MATCH (shop:Shop)
MATCH (shop)<-[:Shops_At]-(customer)
RETURN shop, count(*)

EDIT : 编辑:

Here is a query that will return you shops and the amount of shared customers to other shops in the form : 这是一个查询,它将以以下形式将您的商店以及共享客户的数量返回给其他商店:

s       otherShop  weight
animi   libero  10
animi   modi    9
animi   ut  9
animi   nesciunt    8
animi   libero  6
aut tempore 14
aut animi   17
aut in  15
aut ut  11
aut quo 14


MATCH (s:Shop)
MATCH (s)<-[:SHOPS_AT]-(c)
MATCH (otherShop)<-[:SHOPS_AT]-(c)
WHERE otherShop <> s
WITH s, otherShop, count(distinct(c)) as sharedCustomers
RETURN s, otherShop, sharedCustomers as weight
ORDER BY s.name

You can test this query in the following Neo4j console : http://console.neo4j.org/?id=phsywr 您可以在以下Neo4j控制台中测试此查询: http ://console.neo4j.org/?id=phsywr

Based from this query, you can easily adapt it (after testing the result is what you expect) create relationships between the shops : 根据此查询,您可以轻松地对其进行调整(在测试结果符合预期后),以在商店之间建立关系:

MATCH (s:Shop)
MATCH (s)<-[:SHOPS_AT]-(c)
MATCH (otherShop)<-[:SHOPS_AT]-(c)
WHERE otherShop <> s
WITH s, otherShop, count(distinct(c)) as sharedCustomers
MERGE (s)-[:CROSS_SHOP {weight: sharedCustomers}]->(otherShop)

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

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