1. 程式人生 > >Neo4j 做推薦 (4)—— 基於內容的過濾(續)

Neo4j 做推薦 (4)—— 基於內容的過濾(續)

 

Neo4j 做推薦 (2)—— 基於內容的過濾  前文只是簡單描述了內容過濾的概念和簡單演示。

在此,我們將從具體例項來介紹如何使用基於內容的過濾方法來給使用者推薦電影。

首先,基於共同型別的相似性,如果使用者看過《Inception》,我們就可以給使用者推薦和該電影具有相同分類(流派)的電影。

// Find similar movies by common genres
MATCH (m:Movie)-[:IN_GENRE]->(g:Genre)<-[:IN_GENRE]-(rec:Movie)
WHERE m.title = "Inception"
WITH rec, COLLECT(g.name) AS genres, COUNT(*) AS commonGenres
RETURN rec.title, genres, commonGenres
ORDER BY commonGenres DESC LIMIT 10;

通過上面的Cypher語句,可以很清晰地看到,篩選出 m 的條件是title 屬性為“Inception”,然後通過關係(:IN_GENRE)找出流派(g:Genre),此處注意關係的方向是 -> 。找出 (g:GENRE) 之後,反向查詢 <- 有該流派的所有電影。

在結果返回中,COLLECT 首先把流派名放入一個集合中,然後計算所屬流派的數量(此數量是電影m 和電影rec 具有相同流派的數量)。返回結果再排個序,取最多的前10條。

rec.title genres commonGenres
"Patlabor: The Movie (Kidô keisatsu patorebâ: The Movie)" ["Drama", "Action", "Crime", "Thriller", "Mystery", "Sci-Fi"] 6
"Strange Days" ["Drama", "Action", "Crime", "Thriller", "Mystery", "Sci-Fi"] 6
"Watchmen" ["Drama", "Action", "Thriller", "Mystery", "Sci-Fi", "IMAX"] 6
"Girl Who Played with Fire, The (Flickan som lekte med elden)" ["Drama", "Action", "Crime", "Thriller", "Mystery"] 5
"Fast Five (Fast and the Furious 5, The)" ["Drama", "Action", "Crime", "Thriller", "IMAX"] 5
"Cellular" ["Drama", "Action", "Crime", "Thriller", "Mystery"] 5
"Rubber" ["Drama", "Action", "Crime", "Thriller", "Mystery"] 5
"Negotiator, The" ["Drama", "Action", "Crime", "Thriller", "Mystery"] 5
"X-Files: Fight the Future, The" ["Action", "Crime", "Thriller", "Mystery", "Sci-Fi"] 5
"Source Code" ["Drama", "Action", "Thriller", "Mystery", "Sci-Fi"] 5