Hi Michael,

I think your query would result wrong results. 

Michael - *KNOWS* - Julia - *KNOWS* - Robert - *KNOWS* - Michael (same 
Michael as previous one)

It would consider Robert as a friend's friend for Michael (Michael - Julia 
- Robert). 
However, it is his direct friend ... (Robert - Michael)
I think your solution can't work with cyclic graph ;) 

That's why I used shortestPath function, that clearly avoids that.

What do you think?
 

On Wednesday, March 16, 2016 at 12:03:52 AM UTC+1, Michael Hunger wrote:
>
> 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...@gmail.com 
> <javascript:>> 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+un...@googlegroups.com <javascript:>.
>> 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