elasticsearch 如何按doc_count对区间进行排序?

eqqqjvef  于 2023-01-12  发布在  ElasticSearch
关注(0)|答案(1)|浏览(164)
GET /civile/_search
{
  "size": 0,
  "query": {
    "match": {
      "distretto": "MI"
    }
  },
  "aggs": {
    "our_buckets": {
      "composite": {
        "size": 1000,
        "sources": [
          { "codiceoggetto": { "terms": { "field": "codiceoggetto.keyword", "order": "desc" } } }
        ]
      }
    }
  }
}

我的Elasticsearch查询通过distretto =“MI”匹配文档。当size = 0时,我隐藏结果。
但最重要的是我定义了我们的_buckets聚合,它返回1000个键,并对codiceoggetto.keyword字段进行“分组”。
现在我想按doc_count对我的桶结果排序!我该怎么做?
这里的回应

{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "our_buckets" : {
      "after_key" : {
        "codiceoggetto" : "010001"
      },
      "buckets" : [
        {
          "key" : {
            "codiceoggetto" : "490999"
          },
          "doc_count" : 3
        },
        {
          "key" : {
            "codiceoggetto" : "481312"
          },
          "doc_count" : 1
        },
d7v8vwbk

d7v8vwbk1#

可以使用bucket_sort执行此操作

{
  "size": 0,
  "query": {
    "match": {
      "distretto": "MI"
    }
  },
  "aggs": {
    "our_buckets": {
      "composite": {
        "size": 1000,
        "sources": [
          {
            "codiceoggetto": {
              "terms": {
                "field": "codiceoggetto.keyword",
                "order": "desc"
              }
            }
          }
        ]
      },
      "aggs": {
        "sort_by_count": {
          "bucket_sort": {
            "sort": [
              {
                "_count": {
                  "order": "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

相关问题