【问题标题】:Elastic Search - Aggregation弹性搜索 - 聚合
【发布时间】:2017-05-04 03:29:05
【问题描述】:

我有一个场景,其中父对象有嵌套的对象数组,如下所示

"properties": {
    "id": {
        "type": "integer"
    },
    "schoolProgramAttribute": {
        "type": "nested",
        "properties": {
            "disciplineId": {
                "type": "integer"
            },
            "programId": {
                "type": "integer"
            }
        }
    }
}

现在基本上一个特定的学科ID 可以在嵌套数组中重复多次。要求是计算每个学科 ID 的所有父对象。如果我只是按学科 ID 分组,我没有得到正确的计数,我不确定反向聚合如何提供帮助。当我们尝试使用 parent id 进行反向聚合时,我总是得到 1 的计数,并且我不确定我的方法是否正确。

在 SQL 世界中,它类似于

select count(distinct id),DisciplineId from table group by disciplineid

任何帮助将不胜感激。

【问题讨论】:

  • 您可以添加您正在使用的聚合吗?

标签: elasticsearch aggregate


【解决方案1】:

首先,您应该通过disciplineId 应用嵌套的terms 聚合,并在其中添加reverse_nested

{
    "aggs": {
        "nested": {
            "nested": {
                "path": "schoolProrgamAttribute"
            },
            "aggs": {
                "discipline": {
                    "terms": {
                        "field": "schoolProrgamAttribute.disciplineId"
                    },
                    "aggs": {
                        "parent": {
                            "reverse_nested": {}
                        }
                    }
                }
            }
        }
    }
}

discipline 聚合的结果将为您提供每个 disciplineId 的条目数(并且不排除父项内部的重复项),而 parent 将给出所需的值 - 每个 disciplineId 的父项数价值

【讨论】:

  • 一定会尝试的。谢谢
猜你喜欢
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 2019-11-08
  • 2021-02-19
  • 2023-03-18
  • 2021-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多