如何根据 'buckets' 内的 'alphabetically' 对聚合函数进行排序

How to sort the aggregation function with respect to 'alphabetically' inside the 'buckets'

我的 DSL 查询中有聚合函数,

聚合函数按照'buckets'中的doc_count排序。

如何在'buckets'

中对'alphabetically'的聚合函数进行排序

下面是 DSL 查询

{
  'from': 0,
  'size': 20,
  'aggs': {
    'Employee': {
      'terms': {
        'field': 'employee.name.keyword'
      }
    },
    'Role': {
      'terms': {
        'field': 'role.name.keyword'
      }
    },
    'Designation': {
      'terms': {
        'field': 'designation.name.keyword'
      }
    }
  },
  'query': {
    'bool': {
      'must': [
        {
          'terms': {
            'role.name.keyword': [
              'Developer'
            ]
          }
        }
      ],
      'filter': [
        {
          'term': {
            'parent.keyword': 'Server'
          }
        }
      ]
    }
  }
}

在我的聚合函数中

'aggregations': {'employee': {'doc_count_error_upper_bound': 0,
   'sum_other_doc_count': 0,
   'buckets': [{'key': 'Gold', 'doc_count': 3},
    {'key': 'A', 'doc_count': 1},
    {'key': 'B', 'doc_count': 1}]},
  'designation': {'doc_count_error_upper_bound': 0,
   'sum_other_doc_count': 0,
   'buckets': [{'key': 'Commercial', 'doc_count': 4},
    {'key': 'Enterprise', 'doc_count': 2},
    {'key': 'TEST_BUSINESS_AREA_1', 'doc_count': 1}]},
  'role': {'doc_count_error_upper_bound': 0,
   'sum_other_doc_count': 0,
   'buckets': [{'key': 'role1', 'doc_count': 3},
    {'key': 'role2', 'doc_count': 1}]}}

如果您想按字母顺序(升序)对字段进行排序, 试试下面的查询:

{
  "from": 0,
  "size": 20,
  "aggs": {
    "Employee": {
      "terms": {
        "field": "employee.name.keyword",
        "order": { "_key" : "asc" }           <-- note this
      }
    },
    "Role": {
      "terms": {
        "field": "role.name.keyword",
        "order": { "_key" : "asc" }        <-- note this
      }
    },
    "Designation": {
      "terms": {
        "field": "designation.name.keyword",
        "order": { "_key" : "asc" }             <-- note this
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "role.name.keyword": [
              "Developer"
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "parent.keyword": "Server"
          }
        }
      ]
    }
  }
}