如何在neo4j中将复杂的密码查询更改为简单查询?

如何在neo4j中将复杂的密码查询更改为简单查询?,neo4j,cypher,Neo4j,Cypher,我想找到拥有最大好友数和最小好友数的用户。 我的问题是: "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 min

我想找到拥有最大好友数和最小好友数的用户。 我的问题是:

"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是他的朋友。 但是我使用了三个嵌套查询,我认为它的性能不好。还有其他方法吗?
谢谢。

也许查询两次会更好

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;

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

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

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
类似的方法也应该有效:

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