3

我正在尝试根据用户在此中的共同兴趣来比较用户。
我知道为什么下面的查询会产生重复的对,但在密码中想不出避免它的好方法。有没有办法在不循环密码的情况下做到这一点?

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    |
==> +-----------------------------------------------+ 
4

2 回答 2

13

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

WHERE ID(a) < ID(b)

进行上述查询

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;
于 2013-06-18T10:38:50.967 回答
0

好的,我看到你使用(*)作为起点,这意味着循环整个图形并以每个节点作为起点。所以输出是不同的,而不是像你说的那样重复。

+-----------------------------------------------+
| n.name | other.name | common           | freq |
+-----------------------------------------------+
| "u2"   | "u1"       | ["f1","f2","f3"] | 3    |

不等于:

+-----------------------------------------------+
| n.name | other.name | common           | freq |
+-----------------------------------------------+
| "u1"   | "u2"       | ["f1","f2","f3"] | 3    |

因此,我看到如果您尝试使用索引并设置起点,则不会有任何重复。

start n=node:someIndex(name='C') 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;
于 2013-06-16T08:07:07.007 回答