2016-02-22 50 views
0

我對neo4j比較新,我在合併節點時遇到了一些問題。也許我有錯誤的概念,但這裏是問題陳述和我試圖做的事情。Neo4J基於標籤值合併兩個節點

問題描述:通過根據屬性的值爲每個用戶查找匹配來創建圖表。

Person-> id: user1, movie: lord of the rings 
Person-> id: user1, movie: alice in wonderland 
Person-> id: user2, movie: star wars 
Movie-> name: lord of the rings, genre: Fantasy 
Movie-> name: alice in wonderland, genre: Fantasy 
Movie-> name: star wars, genre: Fantasy 

我想爲我的輸出是:

user1 -> lord of the rings 
     -> alice in wonderland 
user2 -> star wars 

相反,我所得到的是:

user1 -> lord of the rings 
user1 -> alice in wonderland 
user2 -> star wars 

這是我到目前爲止已經試過:

MATCH (a:user),(b:movie) 
where a.movieName = b.name 
MERGE (a)-[r:matches]->(b) 
RETURN r 

理想情況下,我想能夠創建一個圖表,我可以看到用戶對不同電影的所有連接作爲一對多連接。我希望這是明確的!關於我做錯什麼的想法。

回答

0

你在這裏做什麼是一種SQL邏輯,它不是應該使用Neo4j的方式:)。我認爲你必須改變你的數據模型:

其實,你有:

(u:User(name:"", movie:""}) 
(m:Movie{name:""}) 

問題是,在這裏,你有一個:1(或1:2 N永遠不知道哪一個)關係,意義一個用戶只能有一部電影。

Neo4j的是一個圖形數據庫,這意味着你可以簡單地這樣做:

(u:User{properties...})-[:MATCHES]->(m:Movie{properties...}) 

然後你就可以找到每一部電影的用戶,使用該請求:

MATCH (:User{properties...})-[:MATCHES]->(m:Movie) return m; 
+0

所以我是否需要指定它匹配的內容?我的意思是它怎麼知道要匹配什麼? – Roshini

+0

您必須在創建數據集時設置關係,而不必在您的問題查詢中創建外鍵關係。 – Supamiu

+0

我看到工作!非常感謝。 – Roshini