按子聚合按升序对聚合结果进行排序

时间:2019-06-06 18:54:11

标签: elasticsearch

我正在尝试按升序将汇总结果的值排序到另一个汇总结果。

代码:

"aggregation":[
"agg_max_saving_percent"=> [
                    "terms"=> [
                        "field"=> 'key',
                        'size' => 60,
                        'order' => [ 'maximma' => 'desc' ]
                    ],

                    "aggs"=> [
                        "offers"=> [
                            "nested"=> [
                                "path"=> "offers"
                            ],
                            "aggs"=> [
                                "found_savper"=> [
                                    "max"=> [
                                        "field"=> "offers.savper"
                                    ]
                                ]
                            ],

                        ]
                    ],
                    "aggs"=> [  "maximma" => ["max" => ['field' => "sum_score"]]],

                ]
]

映射:

"offers": {
 "type": "nested",
            "properties": {
             "savper": {"type": "long"}
}
}

"sum_score": {"type": "long"},

此刻,我只得到与之进行排序的'MAXIMA'的结果,而不是'found_savper'的结果

尽管,我需要found_savper的结果,并希望通过maxima desc对存储区进行排序。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

对于所需的用例,您需要使用Bucket Sort Aggregation

以下是示例文档,我已实现的汇总查询和响应:

文档:

POST myaggregation/_doc/1
{
  "key": "1001",
  "offers":[
    {
      "savper": 1000
    },
    {
      "savper": 2000
    }
  ],
  "sum_score": 1
}

POST myaggregation/_doc/2
{
  "key": "1001",
  "offers":[
    {
      "savper": 3000
    },
    {
      "savper": 4000
    }
  ],
  "sum_score": 2
}

POST myaggregation/_doc/3
{
  "key": "1002",
  "offers":[
    {
      "savper": 1000
    },
    {
      "savper": 2000
    }
  ],
  "sum_score": 2
}

POST myaggregation/_doc/4
{
  "key": "1002",
  "offers":[
    {
      "savper": 3000
    },
    {
      "savper": 4000
    }
  ],
  "sum_score": 4
}

汇总查询:

POST myaggregation/_search
{
  "size": 0,
  "aggs": {
    "myaggs": {
      "terms": {
        "field": "key",
        "size": 10
      },
      "aggs": {
        "mynested": {
          "nested": {
            "path": "offers"
          },
          "aggs": {
            "found_savper": {
              "max": {
                "field": "offers.savper"
              }
            }
          }
        },
        "maxima":{
          "max": {
            "field": "sum_score"
          }
        },
        "sort": {
          "bucket_sort": {
            "sort": [
              { "maxima": { "order": "desc" }}
            ]
          }
        }
      }
    }
  }
}

请注意,maxima聚合的排序顺序为desc。您可以根据用例将其更改为asc

聚集响应:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "myaggs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1002",
          "doc_count" : 2,
          "maxima" : {
            "value" : 4.0
          },
          "mynested" : {
            "doc_count" : 4,
            "found_savper" : {
              "value" : 4000.0
            }
          }
        },
        {
          "key" : "1001",
          "doc_count" : 2,
          "maxima" : {
            "value" : 2.0
          },
          "mynested" : {
            "doc_count" : 4,
            "found_savper" : {
              "value" : 4000.0
            }
          }
        }
      ]
    }
  }
}

如果删除存储桶排序聚合逻辑,则会观察到聚合将按key进行排序。

希望这会有所帮助!