2016-03-11 73 views
1

我發現類似的電影有以下的Cypher查詢:如何提高Neo4j/Cypher中特定關係的權重?

MATCH (m:Movie)-[r*1..2]-(m2:Movie) 
WHERE m.movieID = '1' 
UNWIND r AS rels 
WITH count(rels) as foo, m2, m 
ORDER BY foo desc 
RETURN DISTINCT m2.title 
LIMIT 25 

基本上找到有共同關係的電影,並返回那些有m最常見的關係,有序的電影。但是,一些關係比其他關係更重要。例如,我想提高[:DIRECTED]關係,以便由同一個導演執導的電影可以在其他人之前返回。我怎樣才能做到這一點?像Dijkstra的算法與:DIRECTED關係有一個低成本?

回答

1

它比這更容易,你可以用CASE的表達式來應用權重。

MATCH (m:Movie)-[r*1..2]-(m2:Movie) 
WHERE m.movieID = '1' 
UNWIND r AS rels 
WITH rels,case type(rels) when "DIRECTED" then 1.2 else 1.0 end as weight 
WITH sum(weight) as foo, m2, m 
ORDER BY foo desc 
RETURN DISTINCT m2.title 
LIMIT 25