简体   繁体   English

意外的neo4j cypher查询结果

[英]Unexpected neo4j cypher query result

I would like to determine the relative percentage of conversation duration with neighbors who know specific person. 我想确定与知道特定人的邻居的会话持续时间的相对百分比。

For example when observing node A first we have to know how much time he spent talking to all of his neighbors which is executed with the following query: 例如,当首先观察节点A时,我们必须知道他花了多少时间与他的所有邻居交谈,这是用以下查询执行的:

neo4j-sh (0)$ start a = node(351061) match (a)-[r:TALKED_TO]->(b) return sum(r.duration)
==> +-----------------+
==> | sum(r.duration) |
==> +-----------------+
==> | 12418           |
==> +-----------------+
==> 1 row, 0 ms

Next we have to check which of his neighbors know specific person (say c) and sum only the durations of conversations among a and b where b knows c: 接下来,我们必须检查他的哪个邻居知道特定的人(比如c),并且只计算a和b之间的对话持续时间,其中b知道c:

neo4j-sh (0)$ start a = node(351061) match (a)-[r:TALKED_TO]->(b)-[p:KNOWS]->(c) return sum(r.duration)
==> +-----------------+
==> | sum(r.duration) |
==> +-----------------+
==> | 21013           |
==> +-----------------+
==> 1 row, 0 ms

What here doesn't seem logical is that the second sum is larger than first one whereas the second one is supposed to be just the part of first. 这里似乎不合逻辑的是第二个和大于第一个,而第二个应该只是第一个的一部分。 Does anyone know what could be the problem for getting such result? 有谁知道得到这样的结果可能是什么问题? The error appeared on 7 users out of 15000. 该错误出现在15000中的7个用户中。

You're not looking at a specific person C in that query. 你不是在查询中查看特定的人C. You're matching all paths to any :KNOWS relationship, so if you have a->b->c and a->b->d your duration between a->b will get counted twice. 你匹配所有路径:KNOWS关系,所以如果你有a-> b-> c和a-> b-> d,你在a-> b之间的持续时间将被计算两次。

What you probably need to do is this instead: 您可能需要做的是:

start a = node(351061), c=node(xxxxx) // set c explicitly
match (a)-[r:TALKED_TO]->(b)
where b-[:KNOWS]->c // putting this in the where clause forces you to set C
return sum(r.duration)

Here's an example in console: http://console.neo4j.org/r/irm0zy 这是控制台中的一个示例: http//console.neo4j.org/r/irm0zy

Remember that match broadens and where tightens the results. 请记住, match变宽where收紧结果。 You can also do this with match , but you need to specify c in start . 您也可以使用match执行此操作,但您需要在start指定c。

A good way to test out what your aggregate functions are doing is to return all of your named variables (or set a path you can return)--this way you see the aggregation separated into subtotals. 测试聚合函数正在执行的操作的一种好方法是返回所有命名变量(或设置可以返回的路径) - 这样您就可以看到聚合分为小计。 Like so: 像这样:

start a=node(1) 
match a-[r:TALKED_TO]->b-[:KNOWS]->c 
return sum(r.duration), a,b,c;
+-----------------------------------------------------------------------------------------------+
| sum(r.duration) | a                       | b                       | c                       |
+-----------------------------------------------------------------------------------------------+
| 20              | Node[1]{name:"person1"} | Node[2]{name:"person2"} | Node[4]{name:"person4"} |
| 20              | Node[1]{name:"person1"} | Node[2]{name:"person2"} | Node[3]{name:"person3"} |
| 20              | Node[1]{name:"person1"} | Node[5]{name:"person5"} | Node[6]{name:"person6"} |
+-----------------------------------------------------------------------------------------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM