(16)ElasticSearch 聚合查询

  1、准备数据

POST /lib/items/_bulk
{"index":{"_id":1}}
{"price":40,"itemID":"ID100123"}
{"index":{"_id":2}}
{"price":50,"itemID":"ID100124"}
{"index":{"_id":3}}
{"price":25,"itemID":"ID100125"}
{"index":{"_id":4}}
{"price":30,"itemID":"ID100126"}
{"index":{"_id":5}}
{"price":null,"itemID":"ID100127"}
GET /lib/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"term":{"price":40}}
            ]
        }
    }
}

GET /lib/items/_search
{
    "query":{
        "bool":{
            "filter":[
                {"terms":{"price":[25,40]}}
            ]
        }
    }
}

  2、操作演示

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

GET /lib/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 /lib/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 /lib/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 /lib/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 /lib/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 /lib/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)先分组在排序

  7.1)主办数据

PUT /lib
{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas":0
      },
        "mappings":{
            "user":{
                "properties":{
                    "name":{"type":"text"},
                    "address":{"type":"text"},
                    "age":{"type":"integer"},
                    "interests":{"type":"text"},
                    "birthday":{"type":"date"}
                }
            }
        }
}
put /lib/user/1
{
    "name":"zhaoliu",
    "address":"hei long jiang sheng tie ling shi",
    "age":50,
    "birthday":"1970-12-12",
    "interests":"xi huang hejiu,duanlian,lvyou"
}

put /lib/user/2
{
    "name":"zhaoming",
    "address":"bei jing hai dian qu qing he zhen",
    "age":20,
    "birthday":"1998-10-12",
    "interests":"xi huan hejiu,duanlian,changge"
}

put /lib/user/3
{
    "name":"lisi",
    "address":"bei jing hai dian qu qing he zhen",
    "age":23,
    "birthday":"1998-10-12",
    "interests":"xi huan hejiu,duanlian,changge"
}

put /lib/user/4
{
    "name":"wangwu",
    "address":"bei jing hai dian qu qing he zhen",
    "age":26,
    "birthday":"1998-10-12",
    "interests":"xi huan biancheng,tingyinyue,lvyou"
}

put /lib/user/5
{
    "name":"zhangsan",
    "address":"bei jing chao yang qu",
    "age":29,
    "birthday":"1988-10-12",
    "interests":"xi huan tingyinyue,changge,tiaowu"
}

  7.2)对那些有唱歌兴趣的用户按年龄分组,并根据每组的平均年龄倒序排序

GET /lib/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
          }
        }
      ]
    }
  }
}
原文地址:https://www.cnblogs.com/javasl/p/11487687.html