[英]How can I use neo4j cypher query to create a histogram of nodes bucketed by number of relationships?
[英]How to create histogram output using neo4j Cypher query?
在我们的图表 model 实体中使用create_time
和delete_time
属性,是否有一种有效的方法来创建将返回计数的查询,类似于直方图,指示实体“存在”的时间段? 时间仓也将是一个变量。 例如,过去 10 年每年,或过去 36 个月每月,或过去 30 天每天。 时间以 integer 格式存储(但如果需要可以更改)。
以下是如何获取create_time
值落在最近 N M 天 windows 内的Entity
节点数量的示例:
WITH timestamp() AS now
MATCH (e:Entity)
WHERE e.create_time > now - ($nPeriods * $periodMillis)
RETURN (now - e.create_time)/$periodMillis AS period, COUNT(*) AS cnt
ORDER BY period
该查询假定您通过periodMillis
和nPeriods
查询参数传递M
和N
例如,如果您希望每个周期为 30 天,并且要查看最后 5 个周期的计数,您可以将30*24*60*60*1000
作为periodMillis
,将 5 作为nPeriods
。
为了加快这个查询,你还应该在:Entity(create_time)
上创建一个索引。
这是一个示例结果(使用上述示例编号):
╒════════╤═════╕
│"period"│"cnt"│
╞════════╪═════╡
│0 │22 │
├────────┼─────┤
│1 │20 │
├────────┼─────┤
│2 │101 │
├────────┼─────┤
│3 │62 │
├────────┼─────┤
│4 │44 │
└────────┴─────┘
期间 0 是过去 30 天(期间结束“现在”,而不是午夜),期间 1 是之前的 30 天,依此类推。 使用午夜作为边界更复杂,可能需要您指定时区并使用时态函数(如 这些)。
[更新]
如果您的期间是一个月,则可以使用 neo4j 时间值和函数来执行符合一个月中实际天数的计算。
例如,如果参数nMonths
提供最大月期数:
WITH date() AS today
MATCH (e:Entity)
WITH duration.inMonths(e.create_time, today).months AS period
WHERE period < $nMonths
RETURN period, COUNT(*) AS cnt
ORDER BY period
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.