Thanks for your suggestion Michael.

However, I managed to do the trick with this query: 

MATCH (person:Person)
WITH person
OPTIONAL MATCH (person)-[:KNOWS]-(p1:Person)
WITH person, COALESCE(COLLECT(p1),[]) AS p1s 
WITH person, CASE p1s WHEN [] THEN [NULL] ELSE p1s END AS p1s
UNWIND p1s AS p1
OPTIONAL MATCH (p1)-[:KNOWS]-(p2:Person)
WHERE NOT ((p2 = person) OR (p2 IN p1s))
WITH person, p1s, COALESCE(COLLECT(distinct p2),[]) AS p2s
WITH person, p1s, CASE p2s WHEN [] THEN [NULL] ELSE p2s END AS p2s 
UNWIND p2s AS p2
OPTIONAL MATCH (p2)-[:KNOWS]-(p3:Person)
WHERE NOT ((p3 = person) OR (p3 IN p1s) OR (p3 IN p2s))
WITH person,
  CASE p1s WHEN [NULL] THEN 0 ELSE SIZE(p1s) END AS f1,
  CASE p2s WHEN [NULL] THEN 0 ELSE SIZE(p2s) END AS f2,
  COUNT(distinct p3) AS f3
RETURN person._firstName + " " + person._lastName, f1, f2, f3, f1+f2+f3 AS total
ORDER BY f1 desc

Very fast :) 

> Le 23 mars 2016 à 02:25, Michael Hunger <michael.hun...@neotechnology.com> a 
> écrit :
> 
> you can also use the path directly then you can specify a minimum length too 
> and you should use a newer version of Neo4j.
> 
> MATCH (person:Person) 
> OPTIONAL MATCH (person)-[:KNOWS]-(p:Person) 
> WITH person, count(p) as f1 
> OPTIONAL MATCH (person)-[:KNOWS*2]-(f2:Person)
> WITH count(distinct f2) as f2, person, f1 
> OPTIONAL MATCH (person)-[:KNOWS*3]-(f3:Person)
> WITH count(distinct f3) AS f3, person, f2, f1 
> RETURN person._firstName + " " + person._lastName, f1, f2, f3
> 
> you should also profile your query, then you see how much data it touches.
> 
> 
> Something else that should work too is:
> 
> MATCH (person:Person) 
> OPTIONAL MATCH path = (person)-[:KNOWS*..3]-(:Person)
> WITH person, count(distinct nodes(path)[1]) as f1, count(distinct 
> nodes(path)[2]) as f2, count(distinct nodes(path)[3]) as f3
> RETURN person._firstName + " " + person._lastName,f1,f2,f3
> 
> Michael
> 
>> Am 16.03.2016 um 00:42 schrieb Michael Azerhad <michael.azer...@gmail.com 
>> <mailto:michael.azer...@gmail.com>>:
>> 
>> shortestPath returns the right results. 
>> Without it, wrong results.
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Neo4j" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to neo4j+unsubscr...@googlegroups.com 
>> <mailto:neo4j+unsubscr...@googlegroups.com>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 
> 
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "Neo4j" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/neo4j/5YkOeCylHEY/unsubscribe 
> <https://groups.google.com/d/topic/neo4j/5YkOeCylHEY/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to 
> neo4j+unsubscr...@googlegroups.com 
> <mailto:neo4j+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to neo4j+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to