简体   繁体   English

neo4j-查找具有强关系的节点

[英]neo4j - find nodes with strong relationship

I have in my graph places and persons as labels, and a relationship "knows_the_place". 我在图形中将地点和人作为标签,并且具有“ knows_the_place”关系。 Like: 喜欢:

(person)-[knows_the_place]->(place) 

A person usually knows multiple places. 一个人通常知道多个地方。

Now I want to find the persons with a "strong" relationship via the places (which have a lot of "places" in common), so for example I want to query all persons, that share at least 3 different places, something like this (not working!) query: 现在,我想通过地点(具有很多“地点”的共同点)找到具有“强”关系的人,例如,我想查询共享至少3个不同地点的所有人。 (不起作用!)查询:

MATCH
(a:person)-[:knows_the_place]->(x:place)<-[:knows_the_place]-(b:person),
(a:person)-[:knows_the_place]->(y:place)<-[:knows_the_place]-(b:person),
(a:person)-[:knows_the_place]->(z:place)<-[:knows_the_place]-(b:person)
WHERE NOT x=y and y=z
RETURN a, b

How can I do this with neo4j Query? 如何使用neo4j Query执行此操作?

Bonus-Question: 奖励-问题:

Instead of showing me the person which have x places in common with another person, even better would be, if I could get a order list like: 如果可以得到像以下这样的订单列表,则不给我显示与另一个人有x个共同点的人,甚至更好。

a shares 7 places with bc shares 5 places with bd shares 2 places with ef shares 1 places with a ... a与BC共享7位,与bd共享5位,与ef共享2位,与a共享1位...

Thanks for your help! 谢谢你的帮助!

Here you go: 干得好:

MATCH (a:person)-[:knows_the_place]->(x:place)<-[:knows_the_place]-(b:person)
WITH a, b, count(x) AS count
WHERE count >= 3
RETURN a, b, count

To order: 订购:

MATCH (a:person)-[:knows_the_place]->(x:place)<-[:knows_the_place]-(b:person)
RETURN a, b, count(x) AS count
ORDER BY count(x) DESC

You can also do both by adding an ORDER BY to the of the first query. 您也可以通过将ORDER BY添加到第一个查询的两者来完成这两个操作。

Keep in mind that this query is a cartesian product of a and b so it will examine every combination of person nodes, which may be not great performance-wise if you have a lot of person nodes. 请记住,这个查询的笛卡尔积ab所以它会检查的每个组合person节点,这可能不是很大的性能,明智的,如果你有很多的person节点。 Neo4j 2.3 should warn you about these sorts of queries. Neo4j 2.3应该警告您有关此类查询的信息。

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

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