【问题标题】:How to sort a composite aggregation on the basis of a sub aggregation ? Below is the query如何根据子聚合对复合聚合进行排序?下面是查询
【发布时间】:2023-03-08 14:34:01
【问题描述】:

GET myIndex/_search
{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user_id": {
              "value": "a88604b0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "entity_status.keyword": {
              "value": "ACTIVE",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "sort": [
    {
      "txn_date": {
        "order": "desc"
      }
    }
  ], 
  "aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "group_by": {
              "terms": {
                "field": "category"
              }
            }
          }
        ]
      },
      "aggs": {
        "total_amount": {
          "sum": {
            "field": "amount"
          }
        }
      }
    }
  }
}

我正在执行上述查询,但我希望聚合按子聚合排序 total_amount 按降序排列。是否有任何修改或其他方式来实现这一点?

这是上述查询的结果。

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 4,
    "successful" : 4,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 22,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_buckets" : {
      "after_key" : {
        "group_by" : "Travel"
      },
      "buckets" : [
        {
          "key" : {
            "group_by" : "Bills"
          },
          "doc_count" : 2,
          "total_amount" : {
            "value" : 86710.44
          }
        },
        {
          "key" : {
            "group_by" : "Grocery"
          },
          "doc_count" : 1,
          "total_amount" : {
            "value" : 43355.22
          }
        },
        {
          "key" : {
            "group_by" : "Fashion"
          },
          "doc_count" : 5,
          "total_amount" : {
            "value" : 216776.1
          }
        },
        {
          "key" : {
            "group_by" : "Recharge"
          },
          "doc_count" : 7,
          "total_amount" : {
            "value" : 303486.54
          }
        },
        {
          "key" : {
            "group_by" : "Shopping"
          },
          "doc_count" : 2,
          "total_amount" : {
            "value" : 86710.44
          }
        },
        {
          "key" : {
            "group_by" : "Travel"
          },
          "doc_count" : 5,
          "total_amount" : {
            "value" : 216776.1
          }
        }
      ]
    }
  }
}

我希望聚合按照total_amount 进行排序。

【问题讨论】:

    标签: java elasticsearch elastic-stack elasticsearch-aggregation aws-elasticsearch


    【解决方案1】:

    很遗憾,目前这是不可能的。每个来源都可以按升序或降序排序,但仅此而已。

    按子聚合排序将需要收集所有复合键并计算每个存储桶的总量,这在内存方面非常昂贵,并且与复合聚合试图实现的完全相反,即一种方式通过内存占用非常低的存储桶进行分页

    另请注意,如果您的类别基数较低(terms 聚合来实现您所需要的,如下所示:

    {
      ...
      "aggs": {
        "group_by": {
          "terms": {
            "field": "category",
            "size": 100,
            "order": {
              "total_amount": "desc"
            }
          },
          "aggs": {
            "total_amount": {
              "sum": {
                "field": "amount"
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 嘿,谢谢你的见解,顺便说一句,我们有什么方法可以在 elasticsearch 中聚合、排序和分页(包括子聚合)?这就是我试图通过上述查询实现的目标
    • 您可以使用简单的桶聚合(例如 termsdate_histogram 但不是复合的)来执行此操作(即按子聚合排序)。
    • 目前,25-30 个类别术语,但将来可以增加到 50 个左右。添加到其他评论中,如果我使用 termsdate_histogram,那么我猜我无法实现分页.
    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-17
    • 2019-04-12
    相关资源
    最近更新 更多