簡體   English   中英

如何在Elasticsearch中對date_histogram聚合的內部聚合排序?

[英]How to sort inner aggregate of date_histogram aggregation in Elasticsearch?

如何編寫一個簡單的Elasticsearch聚合查詢以獲取與此簡單的MySQL查詢等效的內容:

SELECT
    `date`,
    `total`
FROM `table`
GROUP BY `date`
ORDER BY `total` DESC
LIMIT 10;

Elasticsearch中最直接的方法是:

{
    "size": 0,
    "aggregations": {
        "date_agg": {
            "date_histogram": {
                "field": "date",
                "interval": "day"
            },
            "aggregations": {
                "total_agg": {
                    "sum": {
                        "field": "total"
                    }
                }
            }
        }
    }
}

但是,這將使龐大的索引全天都無法使用,這可能是無效的。 另外,我還必須手動找到前10個總和結果。

在Elasticsearch中執行所有這些操作的有效方法是什么?

使用下面的查詢,您可以開始:

{
"size": 0,
"aggregations": {
    "date_agg": {
        "date_histogram": {
            "field": "date",
            "interval": "day",
            "order" : {
                "total_agg" :"desc"   ====> Notice this
            }
        },
        "aggregations": {
            "total_agg": {
                "sum": {
                    "field": "total"
                }
            }
        }
     }
  } 
 }  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM