try this:

MATCH (person:Person)
OPTIONAL MATCH (person)-[:KNOWS]-(f1:Person)-[:KNOWS]-(f2:Person)-[:KNOWS]-(
f3:Person)
WITH person, count(distinct f1) as f1, count(distinct f2) as f2,count(distinct
f3) as f3
RETURN person._firstName + " " + person._lastName, f1, f2, f3

alternatively

MATCH (person:Person)
OPTIONAL MATCH (person)-[:KNOWS]-(f1:Person)-[:KNOWS]-(f2:Person)
WITH person, count(distinct f1) as f1, count(distinct f2) as
f2,collect(distinct f2) as p2
WITH person, f1,f2, reduce(x=0, p in p2 | x + size((p2)-[:KNOWS]-()) ) as f3
RETURN person._firstName + " " + person._lastName, f1, f2, f3



On Tue, Mar 15, 2016 at 10:30 PM, Michael Azerhad <michael.azer...@gmail.com
> wrote:

> Using Neo4J 2.1.5.
>
> Data:
>
>    - 2000 Persons
>    - KNOWS relationships between some of them
>
>
> Goal of the query:
>
> For each person, display her fullname + amount of friends + amount of
> friends' friends +  amount of friends' friends' friends.
>
>
> MATCH (person:Person)
> WITH person
> OPTIONAL MATCH person-[:KNOWS]-(p:Person)
> WITH person, count(p) as f1
> OPTIONAL MATCH path = shortestPath(person-[:KNOWS*..2]-(f2:Person))
> WHERE length(path) = 2
> WITH count(nodes(path)[-1]) AS f2, person, f1
> OPTIONAL MATCH path = shortestPath(person-[:KNOWS*..3]-(f3:Person))
> WHERE length(path) = 3
> WITH count(nodes(path)[-1]) AS f3, person, f2, f1
> RETURN person._firstName + " " + person._lastName, f1, f2, f3
>
>
> This query lasts long ! : 60 seconds !
>
> Is there a tips to optimize it?
>
> Thanks,
>
> Michael
>
> --
> 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.
>

-- 
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