对总和进行排序 - Nest(ElasticSearch)

时间:2016-04-24 15:02:20

标签: c# .net elasticsearch nest elasticsearch-2.0

要求是按员工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
                    }
                 }
              ]
           }
        }
     ]
  }

} }

0 个答案:

没有答案
相关问题