简体   繁体   中英

neo4j - find nodes with strong relationship

I have in my graph places and persons as labels, and a relationship "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:

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?

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:

a shares 7 places with bc shares 5 places with bd shares 2 places with ef shares 1 places with a ...

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.

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. Neo4j 2.3 should warn you about these sorts of queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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