如何使用 neo4j Cypher 查询创建直方图输出?
How to create histogram output using neo4j Cypher query?
在我们的图形模型实体中使用 create_time
和 delete_time
属性,是否有一种有效的方法来创建查询,该查询将 return 计数,类似于直方图,指示在哪个时间箱实体“存在”?时间箱也将是一个变量。例如,过去 10 年每年,或过去 36 个月每月,或过去 30 天每天。时间以整数格式存储(但如果需要可以更改)。
下面是一个例子,说明如何获取 Entity
个节点的数量,其 create_time
值落在最后 N M-day windows:
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
query parameters 传递了 M
和 N
。例如,如果您希望每个周期为 30 天,并且要查看最后 5 个周期的计数,您可以将 30*24*60*60*1000
作为 periodMillis
传递,将 5 作为 nPeriods
传递。
要加快此查询,您还应该在 :Entity(create_time)
上创建一个 index。
这是一个示例结果(使用上述示例编号):
╒════════╤═════╕
│"period"│"cnt"│
╞════════╪═════╡
│0 │22 │
├────────┼─────┤
│1 │20 │
├────────┼─────┤
│2 │101 │
├────────┼─────┤
│3 │62 │
├────────┼─────┤
│4 │44 │
└────────┴─────┘
周期 0 是过去 30 天(周期“现在”结束,而不是午夜),周期 1 是之前 30 天,依此类推。 使用午夜作为边界更复杂,可能需要您指定时区并使用时间函数(如 these)。
[更新]
如果您的经期是一个月,您可以使用 neo4j temporal values and functions 执行符合一个月实际天数的计算。
例如,如果参数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
在我们的图形模型实体中使用 create_time
和 delete_time
属性,是否有一种有效的方法来创建查询,该查询将 return 计数,类似于直方图,指示在哪个时间箱实体“存在”?时间箱也将是一个变量。例如,过去 10 年每年,或过去 36 个月每月,或过去 30 天每天。时间以整数格式存储(但如果需要可以更改)。
下面是一个例子,说明如何获取 Entity
个节点的数量,其 create_time
值落在最后 N M-day windows:
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
query parameters 传递了 M
和 N
。例如,如果您希望每个周期为 30 天,并且要查看最后 5 个周期的计数,您可以将 30*24*60*60*1000
作为 periodMillis
传递,将 5 作为 nPeriods
传递。
要加快此查询,您还应该在 :Entity(create_time)
上创建一个 index。
这是一个示例结果(使用上述示例编号):
╒════════╤═════╕
│"period"│"cnt"│
╞════════╪═════╡
│0 │22 │
├────────┼─────┤
│1 │20 │
├────────┼─────┤
│2 │101 │
├────────┼─────┤
│3 │62 │
├────────┼─────┤
│4 │44 │
└────────┴─────┘
周期 0 是过去 30 天(周期“现在”结束,而不是午夜),周期 1 是之前 30 天,依此类推。 使用午夜作为边界更复杂,可能需要您指定时区并使用时间函数(如 these)。
[更新]
如果您的经期是一个月,您可以使用 neo4j temporal values and functions 执行符合一个月实际天数的计算。
例如,如果参数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