如何在Cypher中汇总集合中的结果?

时间:2014-08-26 22:20:31

标签: neo4j cypher

我正在进行密码查询(Neo4j v2.1.3),我需要聚合来自多个集合的结果,以便在单独的分析中使用。我从以下查询开始,我将返回我们感兴趣的人和特定主机之间的所有Person节点。

MATCH (person:person), (host:person {name:'Host'}),
p = shortestPath((person)-[*]-(host))
WHERE person.name IN ['Steve','Jane']
RETURN collect(extract(n IN nodes(p)| n.name))

这将返回一个集合,其中包含WHERE条件中两个person.name值的结果。

Bob, Jordan, John, Jane, Lisa, Robert, John, Bob, John, Lisa, Bob, Lisa

我想要的是聚合这些值,并列出按降序计数列出的每个数量的计数。

Bob, 3
John, 3
Lisa, 3
Robert, 1
Jordan, 1
Jane, 1

如何在一个cypher语句中添加我的查询来执行此操作?

更新 从@Luanne的回答中,我不得不进行调整以使其正常工作。

MATCH (person:person), (host:person {name:'Host'}),
p = shortestPath((person)-[*]-(host))
WHERE person.name IN ['Steve','Jane']
WITH collect(extract(n IN nodes(p)| n.name)) as nameList 
UNWIND nameList AS name
UNWIND name as ind_name
WITH ind_name, count(*) AS count
RETURN ind_name,count
ORDER BY count DESC

1 个答案:

答案 0 :(得分:2)

您可以unwind收集您的名字:

MATCH (person:person), (host:person {name:'Host'}),
p = shortestPath((person)-[*]-(host))
WHERE person.name IN ['Steve','Jane']
with collect(extract(n IN nodes(p)| n.name)) as nameList 
unwind nameList AS name
WITH name, count(*) AS count
RETURN name,count
ORDER BY count DESC