Reputation: 311
I've got a neo4j database with users, tracks and DJs, where users have a similarity with a DJ, and DJs play tracks. Now I'd like to find all the tracks that are played by the DJ which has a similarity with the user, something like
MATCH (u:User {userId:'6'})-[s:SIMILARITY]->(dj:Dj)-[p:PLAYED]->(t:Track)
return COUNT(t)*s.similarity AS count,t order by count;
Problem is that dj's can play tracks multiple times, which I have represented by multiple PLAYED relations between a DJ and a track. So if a dj plays a track multiple times, it gets more weight by this query, and I'd only like to have those multiple relations count once.
The solution is probably something with DISTINCT, yet I don't know where to place it (count(distinct t) doesn't give the desired result). An alternative would be to restructure the database to only one PLAYED relation, and store the timestamp property of PLAYED as an array.
In that case I'd like to perform a query (somewhere else) asking "find all tracks this dj played after this timestamp", which would also solve this problem, yet I don't know how.
Thanks in advance
Upvotes: 1
Views: 67
Reputation: 39905
Your cypher statement is already pretty close to the solution. However you're aggregating for the track and want to use the count of the relationships to that track was weight, note that I've changed count(t)
to count(p)
:
MATCH (u:User {userId:'6'})-[s:SIMILARITY]->(dj:Dj)-[p:PLAYED]->(t:Track)
return t, COUNT(p)*s.similarity AS weight order by weight;
Upvotes: 2