Elasticsearch - Composite aggregation max value

谁说我不能喝 提交于 2021-01-29 07:11:50

问题


I'm trying to get a max and min value of a date.

From the documentation I don't see a max option in composite: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_value_sources

I'd like something like this:

{
   "size":0,
   "aggs":{
      "intervals":{
         "composite":{
            "size":10000,
            "sources":[
               {
                  "id":{
                     "terms":{
                        "field":"id"
                     }
                  }
               },
               {
                 "minTime": {
                   "min": {
                     "script": "doc['createdAt'].value"
                   }
                 }
               },
               {
                 "maxTime": {
                   "max": {
                     "script": "doc['createdAt'].value"
                   }
                 }
               }
            ]
         }
      }
   }
}

Is it possible to add to this query or will I need a separate query for this?


回答1:


The composite aggregation allows you to paginate over buckets. min and max are metric aggregations that apply on each bucket (i.e. metric aggregations cannot be sources of composite aggregations) and must added as sub-aggregations to the composite aggregation, so what you need to do is the following instead:

{
  "size": 0,
  "aggs": {
    "intervals": {
      "composite": {
        "size": 10000,
        "sources": [
          {
            "id": {
              "terms": {
                "field": "id"
              }
            }
          }
        ]
      },
      "aggs": {
        "minTime": {
          "min": {
            "script": "doc['createdAt'].value"
          }
        },
        "maxTime": {
          "max": {
            "script": "doc['createdAt'].value"
          }
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/64895363/elasticsearch-composite-aggregation-max-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!