如何在Elasticsearch中实现First_value和Last_Value SQL函数?

时间:2019-05-15 23:58:53

标签: java elasticsearch

Elasticsearch JavaAPI支持AggregationBuilder的总和,最小值,最大值,平均数和计数。那么First / First_value和Last / Last_value怎么实现这些功能。

这里是对文档https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_metrics_aggregations.html

的引用

1 个答案:

答案 0 :(得分:0)

您可以在Elasticsearch查询中使用聚合器

"aggs": {
   "FIRST_VALUE": {
      "top_hits": {
         "size": 1,
         "sort": [
            {
               //The string below measures or assess which among the column would you like to arrange in order, 
               //for this example, we presume that there's a "my_date" field in your index
               "my_date": {
                  "order": "asc"
               }
            }
         ]
      }
   },
   "LAST_VALUE": {
      "top_hits": {
         "size": 1,
         "sort": [
            {
               "my_date": {
                  "order": "desc"
               }
            }
         ]
      }
   }
}