0

我想找到最多朋友和最少朋友的用户。这是我的查询:

"start n=node:users({query}) match p=n-[?:Friend*]->x 
with distinct n,count(distinct x) as  cnt  "
+ "start n=node:users({query}) match p=n-[?:Friend*]->x 
with distinct n,count(distinct x) as cnt1, min(cnt) as minnumber "
+ "start n=node:users({query}) match p=n-[?:Friend*]->x
with distinct n,count(distinct x) as friendsNumber, max(cnt1) as 
maxnumber, minnumber  "
+ "where  friendsNumber=minnumber or friendsNumber=maxnumber  return n.name,
friendsNumber"

n 是用户,x 是他的朋友。但我使用了三个嵌套查询,我认为它的性能不好。还有其他方法吗?谢谢。

4

2 回答 2

0

您不想在这里使用可变长度路径。也没有可选的关系,因为您对有朋友的人感兴趣。

我认为在这里运行两个查询可能是最明智的:

start n=node:users({query}) 
match p=n-[:Friend]->x
with ID(n),count(distinct x) as  cnt
with max(cnt) as max,min(cnt) as min

start n=node:users({query}) 
match p=n-[:Friend]->x
with n,count(distinct x) as  cnt
where cnt=max or cnt=min
return n.name

像这样的东西也应该起作用:http ://console.neo4j.org/r/xf3hm0

start n=node(*) 
match p=n-[:KNOWS]-x 
with n,count(distinct x) as  cnt 
with collect([n,cnt]) as data, max(cnt) as max,min(cnt) as min 
return extract(pair in 
   filter(pair in data 
     where tail(pair)=max OR tail(pair)=min) : 
   head(pair)) as person
于 2012-12-16T21:33:47.393 回答
0

也许查询两次会更好

START n=node:users({query})
MATCH n-[:Friend]->x
WITH n,count(x) as cnt
RETURN n, cnt order by cnt desc limit 1;

START n=node:users({query})
MATCH n-[:Friend]->x
WITH n,count(x) as cnt
RETURN n, cnt order by cnt asc limit 1;
于 2012-12-14T13:21:56.897 回答