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

时间:2016-10-21 13:03:48

标签: neo4j 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 个答案:

答案 0 :(得分:1)

谢谢@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

答案 1 :(得分:0)

所以即使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