我很难写一个查询,它将返回有关每个用户的大多数播放曲目的信息。 我喜欢这样的事情:
MATCH (l:Listener)-[lo:LOGS]->(s:Scrobble)-[f:FEATURES]->(t:Track)<-[p:PERFORMS]-(a:Artist)
with l,a,count(*) as numberOfScrobbles
return l.name, a.title, numberOfScrobbles
并获取值列表:用户名 - 艺术家姓名 - 由给定艺术家创建的scrobbled轨道数。
我的目标是为每个用户(每个用户拥有最多scrobbles的艺术家)获得最喜欢的艺术家。我得到的最接近的是:
MATCH (l:Listener)-[lo:LOGS]->(s:Scrobble)-[f:FEATURES]->(t:Track)<-[p:PERFORMS]-(a:Artist)
with l,a,count(*) as numberOfScrobbles
return l.name, max(numberOfScrobbles)
给出了最喜欢的艺术家为给定用户播放的曲目数量,但如何将这个结果的正确艺术家名称加入?
任何线索/提示?
答案 0 :(得分:2)
一个想法(可能有一个更简单的解决方案):
MATCH (l:Listener)-[lo:LOGS]->(s:Scrobble)-[f:FEATURES]->(t:Track)<-[p:PERFORMS]-(a:Artist)
with l,a,count(*) as numberOfScrobbles
with l, collect(a) as artists, collect(numberOfScrobbles) as counts
with l, artists, reduce(x=[0,0], idx in range(0,size(counts)-1) | case when counts[idx] > x[1] then [idx,counts[idx]] else x end)[0] as index
return l.name, artists[index]
reduce
函数用于查找数组中最大元素的位置。然后该索引用于下标artists
数组。
答案 1 :(得分:1)
这是一个应该改进@ StefanAmbruster的答案的查询。它使用MAX()函数找到每个监听器的最大numberOfScrobbles;提取所有为该听众评分最大数字的艺术家;然后返回每个听众,其获奖艺术家的集合,以及最大数量。
MATCH (l:Listener)-[:LOGS]->(:Scrobble)-[:FEATURES]->(:Track)<-[:PERFORMS]-(a:Artist)
WITH l, a, count(*) as numberOfScrobbles
WITH l, collect(a) as artists, collect(numberOfScrobbles) as counts, MAX(numberOfScrobbles) AS max_nos
WITH l, max_nos, extract(i IN range(0, size(counts)-1) | CASE WHEN counts[i] = max_nos THEN artists[i] ELSE NULL END) AS as
RETURN l.name, as, max_nos;