如何在结果中按字段对一组存储桶进行排序

时间:2019-05-15 14:36:34

标签: elasticsearch

我需要按定义为文本的“优先级”字段对存储分区进行排序,但是我不知道该怎么做。

您愿意帮我吗?

我尝试了bucket_sort,但是ES给出了关于类型的错误,与排序和顺序相同。

这是聚合查询

{
 "query": {
   [...]
  },
  "sort": [
    {
      "priority.keyword": {
        "order": "asc"
      }
    }
  ],
  "aggregations": {
    "by_family": {
      "terms": {
        "field": "familyId",
        "size": 25,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      },
      "aggregations": {
        "same_family": {
          "top_hits": {
            "from": 0,
            "size": 1,
            "version": false,
            "explain": false,
            "highlight": {
              "pre_tags": [
                "<search>"
              ],
              "post_tags": [
                "</search>"
              ],
              "fields": {
                "title*": {
                  "type": "unified"
                }
                }
              }
            }
          }
        }
      }
    }
  }

结果示例是:

{
  "responses" : [
    {
      "took" : 13117,
      "timed_out" : false,
      "_shards" : {
        "total" : 10,
        "successful" : 10,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 1754299,
        "max_score" : null,
        "hits" : [...]
      },
      "aggregations" : {
        "by_family" : {
          "doc_count_error_upper_bound" : 40,
          "sum_other_doc_count" : 1753462,
          "buckets" : [
            {
              "key" : 39031576,
              "doc_count" : 92,
              "same_family" : {
                "hits" : {
                  "total" : 92,
                  "max_score" : 10.636923,
                  "hits" : [
                    {
                      "_index" : "idx5-1554993721115",
                      "_type" : "_doc",
                      "_id" : "589403A-333506350",
                      "_score" : 10.636923,
                      "_source" : {
                        "number" : "589403A",
                        "suggest" : {
                          "input" : [
                            "589403A"
                          ]
                        },
                        "id" : "589403A-333506350",
                        "familyRepresentative" : 1,
                        "familyId" : 39031576,
                        "countryCode" : "NZ",
                        "number" : "589403",
                        "kind" : "A",
                        "family" : [ ],
                        "priority" : "20070425", <-------------
                        "created" : "2019-04-14",
                        "modified" : null,
                        "title" : [...],

我想按索引中定义为文本的“优先级”字段对存储桶聚合(asc / desc)进行排序

2 个答案:

答案 0 :(得分:0)

您需要定义另一个子聚合(例如max或min,具体取决于您要排序的方式),然后按该指标对父terms聚合进行排序。请记住,在familyId的存储桶中,文档的priority字段可能都具有不同的值,因此在给定的文档字段上对存储桶进行排序没有任何意义,而仅是对汇总值进行排序给定字段的

{
 "query": {
   [...]
  },
  "sort": [
    {
      "priority.keyword": {
        "order": "asc"
      }
    }
  ],
  "aggregations": {
    "by_family": {
      "terms": {
        "field": "familyId",
        "size": 25,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "max_priority": "desc"
          }
        ]
      },
      "aggregations": {
        "max_priority": {
          "max": {
              "script": "Long.parseLong(doc['priority.keyword'].value)"
          }  
        }
      }
    }
  }

答案 1 :(得分:0)

为简单起见,请尝试以下操作:

'aggs' => [
    'by_family' => [
        'terms' => [
            'field' => 'familyId',
            'order' => [ '_term' => 'asc' ]
        ],
    ],
]

上面的脚本将重点放在您的familyId字段上,然后您可以将此处的_term的值更改为ascdesc以相应地更改顺序