2

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 b c shares 5 places with b d shares 2 places with e f shares 1 places with a ...

Thanks for your help!

dynobo
  • 675
  • 1
  • 6
  • 15
  • Possible duplicate of [neo4j select nodes that have at least n relations](http://stackoverflow.com/questions/30376983/neo4j-select-nodes-that-have-at-least-n-relations) **count** keyword is the key ;) – Mik378 Oct 26 '15 at 01:14
  • Also, small nitpick: Neo4j style is generally to have labels as CamelCase. Here's a guide: http://nigelsmall.com/zen – Brian Underwood Oct 26 '15 at 01:18

1 Answers1

3

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.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • Perfect, thanks! :-) Especially helpful is the 'AS' command, which I somehow missed. This is very useful for this and other of my tasks! But I also noticed the slow performance of such a query. Is there any general way to "monitor" a neo4j-query, to know what makes it slow? Or do I just have to know, what kind of queries are expensive? – dynobo Oct 27 '15 at 09:08
  • You can prepend your queries with `EXPLAIN` and/or `PROFILE`. The `PROFILE` clause will even give a nice visual output in the web console – Brian Underwood Oct 27 '15 at 09:13