要件は、従業員 ID と名前でグループ化し、合計 (給与) を集計することです。私はそれを機能させることができました。ただし、最終的な集計結果は、合計の降順で並べ替える必要があります。いくつかの方法を試しましたが、順序付けがうまくいかないようです。
var resp =
elasticClient.Search<Employee>(
k => k.Size(0).Query(q => q.QueryString(s => s.Query(query)))
.Aggregations(a => a.Terms("EmpId", v => v.Field(f => f.EmpId)
.Aggregations(aa => aa.Terms("EmpName",vv => vv.Field(ff => ff.EmpName).OrderDescending("Salary")
.Aggregations(aaa => aaa.Sum("Salary", sa => sa.Field(fff => fff.Salary))))))));
従業員クラス:
public sealed class Employee
{
public long Id { get; set; }
public int DateId { get; set; }
public int? DivisionNumber { get; set; }
public string Manager { get; set; }
public decimal Salary { get; set; }
public int UpdateId { get; set; }
public DateTime UpdateTimestamp { get; set; }
public int EmpId { get; set; }
public string EmpName { get; set; }
}
マッピング:
POST /sample_employee
{
"mappings": {
"post":{
"properties": {
"empId": {
"type" : "long"
},
"empName": {
"type": "string",
"index": "not_analyzed"
},
"salary": {
"type": "float"
},
"dateId": {
"type": "long"
},
"divisionNumber":{
"type": "long"
}
}
}
}
}
集計のクエリ:
GET /sample_employee/_search
{
"size": 0,
"query": {
"query_string": {
"query": "dateId:(1780) AND divisionNumber:(1)"
}
},
"aggs": {
"EmpId": {
"terms": {
"field": "empId"
},
"aggs": {
"EmpName": {
"terms": {
"field": "empName",
"size": 30,
"order":{
"Amount.value": "desc"}
},
"aggs": {
"Amount": {
"sum": {
"field": "salary"
}
}
}
}
}
}
}
}
応答:
"aggregations": {
"EmpId": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 10,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Jerry Mathews",
"doc_count": 1,
"Amount": {
"value": 10000
}
}
]
}
},
{
"key": 11,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Tom Raju",
"doc_count": 1,
"Amount": {
"value": 15000
}
}
]
}
},
{
"key": 12,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Nel Gad",
"doc_count": 1,
"Amount": {
"value": 20000
}
}
]
}
},
{
"key": 13,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Jim Thomas",
"doc_count": 1,
"Amount": {
"value": 25000
}
}
]
}
},
{
"key": 14,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Amat Ahu",
"doc_count": 1,
"Amount": {
"value": 30000
}
}
]
}
}
]
}
} }