我正在尝试创建一个日期直方图,显示每月的员工数量。
员工映射看起来像这样:
k=A.ja_.at(i)-1
给出这样的开始结束场景(针对三名员工):
{
"number": 1234,
"firstName": "Chris",
"lastName": "Smith",
"employmentDates: [
{
"startDate": "2014-10-03T06:00:00Z",
"endDate": "2017-11-04T06:00:00Z"
}
],
"lastPaidOnDate": "2017-11-10T06:00:00Z",
....
}
我希望直方图与此类似:
|----------------|
|-----------------------------|
|---| |---------------------|
^ ^ ^ ^ ^ ^
似乎我需要在脚本字段上有一个子聚合,但我不知道从哪里开始。
非常感谢您的协助。
答案 0 :(得分:0)
我相信它可以通过使用DateHistogram来完成。但我建议采用一种简单的方法。您必须每次运行一个特定月份的查询:
{
"size": 0,
"aggregations": {
"bool_agg": {
"filter": {
"bool": {
"must": [
{
"range": {
"employmentDates.startDate": {
"lt": "2017-12-01T00:00:00Z"
}
}
},
{
"range": {
"employmentDates.endDate": {
"gte": "2017-11-01T00:00:00Z"
}
}
}
]
}
},
"aggregations": {
"distinct_agg": {
"cardinality": {
"field": "number"
}
}
}
}
}
}
number
计算员工总数请注意,如果employmentDates
包含多条记录,例如:
"employmentDates: [
{
"startDate": "2014-10-03T06:00:00Z",
"endDate": "2017-11-04T06:00:00Z"
}
{
"startDate": "2018-03-03T06:00:00Z",
"endDate": "2018-07-04T06:00:00Z"
}
您必须与Nested Datatype嵌套,可以找到示例here。 并将查询更新为:
{
"size": 0,
"aggregations": {
"nested_agg": {
"nested": {
"path": "employmentDates"
},
"aggregations": {
"bool_agg": {
"filter": {
"bool": {
"must": [
{
"range": {
"employmentDates.startDate": {
"lt": "2017-12-01T00:00:00Z"
}
}
},
{
"range": {
"employmentDates.endDate": {
"gte": "2017-11-01T00:00:00Z"
}
}
}
]
}
},
"aggregations": {
"comment_to_issue": {
"reverse_nested": {},
"aggregations": {
"distinct_agg": {
"cardinality": {
"field": "number"
}
}
}
}
}
}
}
}
}
}