至少有两个属性

时间:2013-08-02 20:24:59

标签: neo4j cypher

我的图表看起来像this

a - [:LIKES {frequency:6}] - > ice b - [:LIKES {frequency:3}] - >披萨等

我正在运行此查询,以找出两个人喜欢的常见食物。如果我将频率属性加起来为食物创建一个like-index,它就可以正常工作。

START b=node:node_auto_index(name='B') 
MATCH p = b-[r1:LIKES]->stuff, someone_else_too -[r2:LIKES]->stuff 
WITH b, someone_else_too, r1.frequency + r2.frequency AS likeIndex 
RETURN likeIndex ,someone_else_too 
ORDER BY likeIndex DESC

但是,我想要做的是找到两个频率值中的最小值(不是总数的最小值)。所以像这样的事情(r1.frequency,r2.frequency)。目标是消除偏斜,如果一对中的一个人对特定食物具有非常高的亲和力,则会增加类似排名。

    START b=node:node_auto_index(name='B') 
    MATCH p = b-[r1:LIKES]->stuff, someone_else_too -[r2:LIKES]->stuff 
    WITH b, someone_else_too, min(r1.frequency,r2.frequency) AS likeIndex 
    RETURN likeIndex ,someone_else_too 
    ORDER BY likeIndex DESC

但是,Cypher不会接受这种语法。任何建议或解决方法?请参阅Neo4j控制台here

3 个答案:

答案 0 :(得分:1)

在2.0中,您可以使用case/whenhttp://console.neo4j.org/r/y8sqi1

START b=node:node_auto_index(name='B') 
MATCH p = b-[r1:LIKES]->stuff, someone_else_too -[r2:LIKES]->stuff 
WITH b, someone_else_too, 
  CASE WHEN r1.frequency < r2.frequency 
    THEN r1.frequency 
    ELSE r2.frequency 
  END AS likeIndex 
RETURN likeIndex ,someone_else_too 
ORDER BY likeIndex DESC

答案 1 :(得分:0)

有一种获得最小值的数学方法:

(r1.frequency + r2.frequency - ABS(r1.frequency - r2.frequency))/2

但它太沉重了:(

答案 2 :(得分:0)

你可以使用 apoc.coll.min:

  START b=node:node_auto_index(name='B') 
MATCH p = b-[r1:LIKES]->stuff, someone_else_too -[r2:LIKES]->stuff 
WITH b, someone_else_too, apoc.coll.min([r1.frequency, r2.frequency]) AS likeIndex 
RETURN likeIndex ,someone_else_too 
ORDER BY likeIndex DESC