Neo4j - 根据标签获取电影推荐

时间:2016-09-20 05:17:53

标签: neo4j cypher neo4j-cql

我想查找搜索标签包含电影标签的电影,由我观看。以下是我的查询,但它没有产生预期结果,任何人都可以让我知道这个查询有什么问题吗?

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

我尝试了这个查询并且它正在运行,但我不知道如何使其成为动态tagid?我是neo4j的新手,我在neo4j中读了 WITH 语句,但不知道如何使用它以及它是否有效。

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)

1 个答案:

答案 0 :(得分:4)

// 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

或带有模式的变体:

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