聚合查询-桶聚合

相当于sql group by

1.auto_date_histogram 自动日期间隔直方图
{
    "aggs" : {
        "sales_over_time" : {
            "auto_date_histogram" : {
                "field" : "insert_time",
                "format" : "yyyy-MM-dd HH:mm:ss"
            }
        }
    },
    "size":0
}
2.composite 多字段聚合
相当于sql group by a,b,c....
{
    "aggs": {
        "my_buckets": {
            "composite": {
                "sources": [
                    {
                        "namess": {
                            "terms": {
                                "field": "name.keyword"
                            }
                        }
                    }
                ]
            }
        }
    },
    "size": 0
}
3.date_histogram 日期间隔直方图
{
    "aggs" : {
        "sales_over_time" : {
            "date_histogram" : {
                "field" : "insert_time",
                "calendar_interval" : "year", //自定义间隔只能用fixed_interval (20d)
                "format" : "yyyy-MM-dd" 
            }
        }
    },
    "size":0
}
4.date_range 时间范围
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "insert_time",
                "format": "yyyy-MM-dd",
                "ranges": [
                    { "from": "2018-06-21","to":"now" }, //相当于sql   between and
                    {"from":"2018-06-21"}, //  >
                    {"to":"now"}  //  <
                ]
            }
        }
    },
    "size":0
}
5.filter 筛选聚合
{
    "aggs": {
        "t_shirts": {
            "filter": {
                "term": {
                    "id": "3"
                }
            },
            "aggs": {
                "avg_price": {
                    "avg": {
                        "field": "id"
                    }
                }
            }
        }
    },
    "size": 0
}
6.filters 多筛选
{
    "aggs" : {
    "aaa" : {
      "filters" : {
        "filters" : {
          "1" :   { "term" : { "id" : "1"}},
          "2" :   { "term" : { "id" : "2"}}
        }
      }
    }
  },
   "size": 0
}
7.histogram 数字间隔直方图
{
   "aggs" : {
        "id" : {
            "histogram" : {
                "field" : "id",
                "interval" : 50
            }
        }
    },
   "size": 0
}
8.histogram 数字间隔直方图
{
   "aggs" : {
        "id" : {
            "histogram" : {
                "field" : "id",
                "interval" : 50
            }
        }
    },
   "size": 0
}
9.missing 缺少聚合
{
    "aggs" : {
        "no" : {
            "missing" : { "field" : "province.keyword" }
        }
    },
   "size": 0
}
10.missing 缺少聚合
{
    "aggs" : {
        "no" : {
            "missing" : { "field" : "province.keyword" }
        }
    },
   "size": 0
}
11.range 数字范围聚合
{
   "aggs" : {
        "ids" : {
            "range" : {
                "field" : "id",
                "ranges" : [
                    { "to" : 100 },
                    { "from" : 200, "to" : 400 },
                    { "from" : 600 }
                ]
            }
        }
    },
   "size": 0
}
12.term 术语汇总
{
   "aggs" : {
        "genres" : {
            "terms" : { "field" : "type.keyword" } 
        }
    },
   "size": 0
}

你可能感兴趣的:(聚合查询-桶聚合)