Cypher:返回具有唯一节点的集合

这是一个查询,返回与他们的文凭集合不同的单词

MATCH (diploma:Diploma)-->(w:Word) 
RETURN DISTINCT w, COLLECT(diploma) as diplomas, COUNT(diploma) as nb_diplomas
ORDER BY nb_diplomas DESC

(编辑:删除OPTIONAL MATCH并在COUNT中使用文凭而不是用户,删除简历)

文凭可以包含多个Word,并将出现在多个馆藏中 . 我希望这个查询在第一个集合中只返回一次文凭,这意味着包含最多文凭节点的集合

回答(2)

3 years ago

谢谢@InverseFalcon的建议,感谢UNWIND功能,我终于找到了自己的方式

MATCH (diploma:Diploma)-->(w:Word) 
WITH DISTINCT w, COLLECT(diploma) AS diplomas 
WITH w, diplomas, LENGTH(diplomas) as nb 
ORDER BY nb DESC 
UNWIND diplomas as diploma 
WITH DISTINCT diploma, HEAD(COLLECT(w)) as w, MAX(nb) as nb
WITH DISTINCT w, COLLECT(diploma) as diplomas 
WITH w, diplomas, LENGTH(diplomas) as nb  
ORDER BY nb DESC 
RETURN w, diplomas, nb

3 years ago

所以即使a:文凭与许多人相关:单词,你只对以下内容感兴趣:单词最常用于:文凭,然后输出每行:Word和相关:基于之前过滤频率的文凭?

这可能是一个好的开始:

MATCH (diploma:Diploma)-->(w:Word) 
WITH diploma, w, COUNT(w) as frequency
WITH diploma, w 
WHERE frequency = MAX(frequency)
RETURN w, COLLECT(diploma) as diplomas, COUNT(diploma) as nb_diplomas
ORDER BY nb_diplomas DESC

请记住,如果文凭中有多个具有相同最大数量的单词,那么该文凭将与这两个单词相关联,并在最后出现在两个集合中 .

要在需要时解决这个问题,您需要在查询中间选择那些最大单词集合的头部,例如:

MATCH (diploma:Diploma)-->(w:Word) 
WITH diploma, w, COUNT(w) as frequency
WITH diploma, w 
WHERE frequency = MAX(frequency)
WITH diploma, COLLECT(w) as words
WITH diploma, HEAD(words) as w
RETURN w, COLLECT(diploma) as diplomas, COUNT(diploma) as nb_diplomas
ORDER BY nb_diplomas DESC