当对序在密码中不重要时,查询唯一的节点对

use*_*018 3 neo4j cypher

我试图根据用户在此图中的共同兴趣来比较用户.
我知道为什么以下查询会产生重复对,但是在cypher中无法想到避免它的好方法.如果没有在密码中循环,有没有办法做到这一点?

neo4j-sh (?)$ start n=node(*) match p=n-[:LIKES]->item<-[:LIKES]-other where n <> other return n.name,other.name,collect(item.name) as common, count(*) as freq order by freq desc;
==> +-----------------------------------------------+
==> | n.name | other.name | common           | freq |
==> +-----------------------------------------------+
==> | "u1"   | "u2"       | ["f1","f2","f3"] | 3    |
==> | "u2"   | "u1"       | ["f1","f2","f3"] | 3    |
==> | "u1"   | "u3"       | ["f1","f2"]      | 2    |
==> | "u3"   | "u2"       | ["f1","f2"]      | 2    |
==> | "u2"   | "u3"       | ["f1","f2"]      | 2    |
==> | "u3"   | "u1"       | ["f1","f2"]      | 2    |
==> | "u4"   | "u3"       | ["f1"]           | 1    |
==> | "u4"   | "u2"       | ["f1"]           | 1    |
==> | "u4"   | "u1"       | ["f1"]           | 1    |
==> | "u2"   | "u4"       | ["f1"]           | 1    |
==> | "u1"   | "u4"       | ["f1"]           | 1    |
==> | "u3"   | "u4"       | ["f1"]           | 1    |
==> +-----------------------------------------------+ 
Run Code Online (Sandbox Code Playgroud)

Pet*_*uer 11

为了避免以a--b和的形式出现重复b--a,您可以在WHERE子句中排除其中一个组合

WHERE ID(a) < ID(b)
Run Code Online (Sandbox Code Playgroud)

进行上述查询

start n=node(*) match p=n-[:LIKES]->item<-[:LIKES]-other where ID(n) < ID(other) return n.name,other.name,collect(item.name) as common, count(*) as freq order by freq desc;
Run Code Online (Sandbox Code Playgroud)