如何在固定数量的存储桶中平均分配弹性搜索结果?

时间:2019-07-24 19:25:34

标签: elasticsearch

假设我有一个包含20个文档的Elastic Search索引 每份文件代表一名雇员及其年龄和部门 我需要能够将结果平均分配到5个桶中 因此,每个存储区包含4名员工,对于每个存储区,我需要知道该范围的最小和最大年龄值,以及每个部门中有该范围内人员的人数。

预期值应类似于 ...

"aggregations": {
    "age_ranges": {
        "buckets": [{
            "key": "range 1",
            "min_age": 18,
            "max_age": 25,
            "doc_count": 4,
            "department_distribution": [{
                "department": "ACCOUNTING",
                "employees_in_the_range": 2
            }, {
                "department": "SALES",
                "employees_in_the_range": 1
            },
                {
                    "department": "IT",
                    "employees_in_the_range": 1
                }
            ]
        },
            {
                "key": "range 2",
                "min_age": 25,
                "max_age": 30,
                "doc_count": 4,
                "department_distribution": [{
                    "department": "IT",
                    "employees_in_the_range": 4
                }]
            },
            {
                "key": "range 3",
                "min_age": 30,
                "max_age": 40,
                "doc_count": 4,
                "department_distribution": [{
                    "department": "ACCOUNTING",
                    "employees_in_the_range": 3
                },
                    {
                        "department": "IT",
                        "employees_in_the_range": 1
                    }
                ]
            },
            {
                "key": "range 4",
                "min_age": 40,
                "max_age": 53,
                "doc_count": 4,
                "department_distribution": [{
                    "department": "ACCOUNTING",
                    "employees_in_the_range": 1
                },
                    {
                        "department": "SALES",
                        "employees_in_the_range": 3
                    }
                ]

            },
            {
                "key": "range 5",
                "min_age": 53,
                "max_age": 64,
                "doc_count": 4,
                "department_distribution": [{
                    "department": "ACCOUNTING",
                    "employees_in_the_range": 4
                }]

            }
        ]
    }
}

查询应该是什么样?我当时在考虑使用百分位数聚合,但不确定执行该操作是否正确

0 个答案:

没有答案