弹性搜索 - 聚合

时间:2017-05-04 03:29:05

标签: elasticsearch aggregate

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

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

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

在SQL世界中它就像

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

首先,您应该在'defaults' => [ 'users'=>[ 'guard' => 'users', 'passwords' => 'users' ], 'branch'=>[ 'guard' => 'branch', 'password' => 'branch' ] ], 'guards' => [ 'users' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'branch'=> [ 'driver' => 'session', 'provider' => 'branch', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'branch' => [ 'driver' => 'eloquent', 'model' => App\Branch::class, ], ], 之前应用嵌套的terms聚合,并在其中添加disciplineId

reverse_nested

{ "aggs": { "nested": { "nested": { "path": "schoolProrgamAttribute" }, "aggs": { "discipline": { "terms": { "field": "schoolProrgamAttribute.disciplineId" }, "aggs": { "parent": { "reverse_nested": {} } } } } } } } 聚合的结果将为您提供每个discipline的条目数(并且不排除父级内的重复项),而disciplineId将提供所需的值 - 每个parent值的父母

相关问题