Question

I have the following SQL code that does exactly what I want. I want to try doing the same using Cypher.

select distances.userid as userid, dist/(sqrt(my.norm)*sqrt(users.norm)) as score
    from (select userid, 
    sum((user.rating)*(ratings.rating)) as dist
    from ratings, user
    where user.itemid = ratings.itemid
    group by userid) as distances, 
    (select userid, sum((rating)*(rating)) as norm 
    from ratings
    group by userid) as users, 
    (select sum((rating)*(rating)) as norm 
    from user) as my 
    where users.userid = distances.userid 
    order by score desc 
    limit 80;

In my cypher graph, the cypher relationship "RATES" between user and item contains the user rating. The cypher node user contains the userid and users average rating, which in sql is help within ratings.rating.

I've attempted to implement each subquery separately but am having trouble since each seems to depend on the next and I'm unsure how to link my cypher queries.

How do I implement this in cypher?

Edit-------------------

Managed to translate the last sub query.

SQL

select sum((rating)*(rating)) as norm 
from user

Cypher

MATCH (n)-[r:RATES]->(m)
WHERE n.userid = '1'
RETURN sum((toInt(r.rating))*(toInt(r.rating))) as norm

Still unsure how to use this in the main query.

Was it helpful?

Solution

You use the WITH statement to join subqueries.

To quote from the neo4j cheat sheet:

The WITH syntax is similar to RETURN. It separates query parts explicitly, allowing you to declare which identifiers to carry over to the next part.

The neo4j manual page for WITH is here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top