首页 > 解决方案 > 如何在 ES 存储桶中填充缺失的数据以与之前的聚合?

问题描述

我试图弄清楚如何在 ES 中进行这种聚合,我需要将丢失的数据填充到存储桶中。

假设我的文档看起来像这样(非常简化):

Score {
orgId: Long
date: Date
score: Float
status: ("draft" | "complete")
}

我想要的是组织的最后得分状态为“完成”的组织每天的平均得分......可能不是每天都有得分,但我想包括所有后续日子的最后得分,如果它在“完成”状态。如果我们稍后获得“草稿”状态分数,我不想将该组织包括在该日期和后续日期的平均值中,直到有另一个状态为“已完成”的分数。

所以这里有一个例子:

{orgId: 1, date: '5/19/2021', score: 0.1, status: 'complete'}
{orgId: 2, date: '5/19/2021', score: 0.2, status: 'complete'}
{orgId: 3, date: '5/19/2021', score: 0.5, status: 'complete'}
{orgId: 1, date: '5/21/2021', score: 0.1, status: 'draft'}
{orgId: 1, date: '5/23/2021', score: 0.5, status: 'complete'}
{orgId: 1, date: '5/25/2021', score: 0.5, status: 'draft'}

所以为此我想回来:

{date: '5/19/2021', avg: 0.266667}
{date: '5/20/2021', avg: 0.266667}
{date: '5/21/2021', avg: 0.35}
{date: '5/22/2021', avg: 0.35}
{date: '5/23/2021', avg: 0.4}
{date: '5/24/2021', avg: 0.4}
{date: '5/25/2021', avg: 0.35}

我对 ES 很陌生,所以我对如何实现这一点有点迷茫。任何帮助表示赞赏。

标签: elasticsearch

解决方案


推荐阅读