0
votes

I am using Neo4J to process a data set having users and their rankings for movies. I have two node types - movies and users and a single relationship giving the rank of the user to a movie.

My goal is to explore the prediction ability for a user based on the similarity between him/her and other users.

To do that I would like to calculate similarity between users as the normalized sum of rankings for movies they both viewed. This information should then be stored in a relationship between the users.

I have the following Cypher query work for two particular nodes:

MATCH (a)-[r1:RANKS]->(m), (b)-[r2:RANKS]->(m) where a.ID<>b.ID return a.ID,b.ID, sum(abs(r1.Rate-r2.Rate))/count(m)

I would like to use this query to also create a relationship but as soon as I try the following:

MATCH (a)-[r1:RATES]->(m)<-[r2:RATES]-(b) where a.ID<>b.ID and a.ID="u_1" and b.ID="u_753" CREATE a-[:SIMILARITY_RANK{Similarity:(abs(r1.Rank-r2.Rank))/count(m)}]->(b)

I get the error message that I am using the count function in the wrong context. What am I doing wrong?

Dotan

2

2 Answers

1
votes

Works for me (just cleaned up a bit):

MATCH (a:User)-[r1:RANKS]->(m:Movie)<-[r2:RANKS]-(b:User) 
WHERE a.ID<>b.ID 
RETURN a.ID,b.ID, sum(abs(r1.Rate-r2.Rate)) / count(m)

see: http://console.neo4j.org/r/4zb8fg

for creating your rel I'd do

MATCH (a:User)-[r1:RANKS]->(m:Movie)<-[r2:RANKS]-(b:User) 
WHERE a.ID<>b.ID 
WITH a,b, sum(abs(r1.Rate-r2.Rate)) / count(m) as similarity
CREATE a-[:SIMILARITY_RANK {Similarity:similarity}]->(b)

Also worked for me running this in the console above

MATCH (a:User)-[r1:RANKS]->(m:Movie)<-[r2:RANKS]-(b:User) 
WHERE a.name<>b.name 
WITH a,b, sum(abs(r1.Rate-r2.Rate))/ count(m) AS similarity 
CREATE a-[:SIMILARITY_RANK { Similarity:similarity }]->(b)
0
votes

Please try:

MATCH (a)-[r1:RATES]->(m)<-[r2:RATES]-(b)
where a.ID<>b.ID and a.ID="u_1" and b.ID="u_753"
with count(m) as count,a,b
CREATE a-[:SIMILARITY_RANK{Similarity:(abs(r1.Rank-r2.Rank))/count}]->(b)

count cannot be used in the create