Sachin Vairagi
Sachin Vairagi

Reputation: 5344

Neo4j - get movie recommendation based on tags

I want to Find movies whose search tag contains the tags of movie , watched by me. Following is my query but it is not producing expected result , can anybody please let me know what is wrong with this query ?

MATCH (m:Movie)-[:HAS_TAG]->(t:Tag)
    WHERE NOT (:Person {personId : '50'})-[:WATCHED]->(m)
    AND (m)-[:HAS_TAG]->(t)
    RETURN DISTINCT(m.movieId) as movieId,
    COLLECT(t.tagId) as Tag

I tried this query and it is working but I don't know how to make it dynamic tagid ? I am newbie in neo4j , I read WITH statement in neo4j but not sure how to use it and whether it will work or not.

MATCH (m:Movie)-[:HAS_TAG]->(t:Tag)
WHERE NOT (:Person {personId : '50'})-[:WATCHED]->(m)
AND t.tagId in ['16','19','21','22','23','24','25'] 
RETURN DISTINCT(m.movieId)

Upvotes: 4

Views: 256

Answers (1)

stdob--
stdob--

Reputation: 29172

// Collect all films and tags that apply to films that are watched by:
//
MATCH (p:Person {personId : '50'})
WITH p
MATCH (p)-[:WATCHED]->(m:Movie)-[:HAS_TAG]->(t:Tag)
WITH p, collect(distinct m) AS movies, collect(distinct t) AS tags
//
// And get recommendation:
//
MATCH (m:Movie)-[:HAS_TAG]->(t:Tag) 
WHERE NOT m IN movies AND t IN tags
RETURN distinct m AS movies

Or the variant with a pattern:

MATCH (p:Person {personId : '50'})
WITH p
MATCH (p)-[:WATCHED]->(wm:Movie)-[:HAS_TAG]->(t:Tag)<-[:HAS_TAG]-(rm:Movie)
WHERE NOT (p)-[:WATCHED]->(rm)
RETURN distinct rm AS movies

Upvotes: 5

Related Questions