在1个密码查询中从多个关系中提取权重

时间:2015-09-01 12:51:30

标签: neo4j cypher

我想计算一个"公司指数"对于特定用户,基于用户查看或更新文件的次数。为了得到这个,我会为某些路径分配值。这是一个例子:

(u1:User {name: 'Alice'})-[:UPDATED]->(f:File)<-[:VIEWED]-(u2:User) // is worth 0.2 points
(u1:User {name: 'Alice'})-[:VIEWED]->(f:File)<-[:VIEWED]-(u2:User) // is worth 0.1 points
(u1:User {name: 'Alice'})-[:VIEWED]->(f:File)<-[:UDATED]-(u2:User) // is worth 0.2 points
(u1:User {name: 'Alice'})-[:UPDATED]->(f:File)<-[:UPDATED]-(u2:User) // is worth 0.5 points

图像显示了可能的图表。 a possible graph

我想知道查询必须如何返回以下结果。

User: Alice, User: Charly, index: (3 * 0.2) // 3 because there are 3 matching paths (Relationship with the lowest weight in the path)
User: Alice, User: Bob, index: (3 * 0.1)

这是我到目前为止所做的:

MATCH (u1:User {name:'Alice'})-[r1:VIEWED]->(f:File)<-[r2:UPDATED]-(u2:User)
OPTIONAL MATCH (u1:User {name:'Alice'})-[r3:VIEWED]->(f:File)<-[r4:VIEWED]-(u3:User)
RETURN u2.name, min(r1.weight) AS ViewUpd, u3.name, min(r3.weight) AS ViewView

此查询根本不起作用,但我希望它能澄清我想要的内容。

1 个答案:

答案 0 :(得分:1)

[EDITED]

此查询是否符合您的要求?

MATCH (u1:User { name:'Alice' })-[r1]->(f:File)<-[r2]-(u2:User)
RETURN
  u2.name AS name,
  SUM(
    r1.weight * (CASE
      WHEN (TYPE(r1)= "VIEWED" AND TYPE(r2)= "VIEWED") THEN 0.1
      WHEN (TYPE(r1)= "UPDATED" AND TYPE(r2)= "UPDATED") THEN 0.5
      ELSE 0.2
      END)) AS index;

Here is a console显示此查询,以下是示例结果:

+--------------------------------+
| name      | index              |
+--------------------------------+
| "Bob"     | 0.6000000000000001 |
| "Elvis"   | 1.5                |
| "David"   | 0.6000000000000001 |
| "Charley" | 2.1                |
+--------------------------------+

在我的样本数据中,&#34; Charley&#34;与UPDATEDVIEWED关系且File关系为UPDATED&#34; Alice&#34;。 Charley的结果index是这两种关系的索引值的总和。