码迷,mamicode.com
首页 > 其他好文 > 详细

(16)ElasticSearch 聚合查询

时间:2019-09-08 20:36:33      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:esc   inter   too   key   bsp   desc   sum   cse   lib   

数据准备:第15节的数据

(1)求和(sum),aggs是固定写法,price_of_sum是取的名字。

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_sum":{
            "sum":{
                "field":"price"
            }
        }
    }
}

输出结果如下:

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_sum": {
      "value": 145
    }
  }
}

(2)求最小值(min)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_min":{
            "min":{
                "field":"price"
            }
        }
    }
}

输出结果如下:

{
  "took": 51,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_min": {
      "value": 25
    }
  }
}

(3)求最大值(max)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_max":{
            "max":{
                "field":"price"
            }
        }
    }
}

输出结果如下:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_max": {
      "value": 50
    }
  }
}

(4)求平均值(avg)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_avg":{
            "avg":{
                "field":"price"
            }
        }
    }
}

输出结果如下:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_avg": {
      "value": 36.25
    }
  }
}

(5)求基数(cardinality),互不相同的个数

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_cardi":{
            "cardinality":{
                "field":"price"
            }
        }
    }
}

执行结果如下:

{
  "took": 38,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_cardi": {
      "value": 4
    }
  }
}

(6)分组(terms)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_group_by":{
            "terms":{
                "field":"price"
            }
        }
    }
}

输出结果如下:

{
  "took": 45,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_group_by": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 25,
          "doc_count": 1
        },
        {
          "key": 30,
          "doc_count": 1
        },
        {
          "key": 40,
          "doc_count": 1
        },
        {
          "key": 50,
          "doc_count": 1
        }
      ]
    }
  }
}

(7)对那些有唱歌兴趣的用户按年龄分组,并根据每组的平均年龄倒序排序,数据来自第12节

GET /lib3/user/_search
{
    "size":0,
    "query":{
        "match":{
            "interests":"changge"
        }
    },
    "aggs":{
        "age_of_group":{
            "terms":{
                "field":"age",
                "order":{
                    "age_of_avg":"desc"
                }
            },
            "aggs":{
                "age_of_avg":{
                    "avg":{
                        "field":"age"
                    }
                }
            }
        }
    }
}

输出结果如下:

{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "age_of_group": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 29,
          "doc_count": 1,
          "age_of_avg": {
            "value": 29
          }
        },
        {
          "key": 23,
          "doc_count": 1,
          "age_of_avg": {
            "value": 23
          }
        },
        {
          "key": 20,
          "doc_count": 1,
          "age_of_avg": {
            "value": 20
          }
        }
      ]
    }
  }
}

 

(16)ElasticSearch 聚合查询

标签:esc   inter   too   key   bsp   desc   sum   cse   lib   

原文地址:https://www.cnblogs.com/javasl/p/11487687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!