zoukankan      html  css  js  c++  java
  • Elasticsearch学习笔记(三)聚合分析Agg

    一、设置fielddata

    PUT /index/_mapping/type
    {
         "properties":{
              "fieldName":{
                 "type":"text",
                 "fielddata":true
             }
         }
    }
    例如:
    PUT /ecommerce/_mapping/product
    {
       "properties": {
         "tags":{
           "type": "text",
           "fielddata": true
         }
       }
    }

    二、聚合分析

    1、聚合分析基本语法:


         需求1:计算每个tag下的商品数量
    GET /ecommerce/product/_search          // GET /index/type/_search
    {
       "size": 0,   // 返回数据hits内不显示命中的数据记录
       "aggs": {
         "all_tags": {  // 聚合器名称
           "AGG_TYPE": {      //AGG_TYPE聚合类型:terms,avg
             "field": "fieldName"//字段名称
           }
         }
       }
    }

    2、为聚合分析添加刷选条件


         需求2:对名称中包含yagao的商品,计算每个tag下的商品数量

    GET /ecommerce/product/_search
    {
       "size": 0,
       "query": {
         "match": {
           "name": "yaogao"
         }
       },
       "aggs": {
         "all_tags": {
           "terms": {
             "field": "tags"
           }
         }
       }
    }


    3、嵌套聚合分析


         需求3:先分组,再算每组的平均值,计算每个tag下的商品的平均价格

    GET /ecommerce/product/_search
    {
       "size": 0,
       "aggs": {
         "group_by_tags": {
           "terms": {
             "field": "tags"
           },
           "aggs": {
             "avg_price": {
               "avg": {
                 "field": "price"
               }
             }
           }
         }
       }
    }


    4、聚合结果排序


    需求4:计算每个tag下的商品的平均价格,并且按照平均价格降序排序
    GET /ecommerce/product/_search
    {
       "size": 0,
       "aggs": {
         "all_tags": {
           "terms": {
             "field": "tags",
             "order": {
               "avg_price": "asc"
             }
           },
           "aggs": {
             "avg_price": {
               "avg": {
                 "field": "price"
               }
             }
           }
         }
       }
    }

    需求5:按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格
    GET /ecommerce/product/_search
    {
       "size": 0,
       "aggs": {
         "group_by_price": {
           "range": {
             "field": "price",
             "ranges": [
               {
                 "from": 0,
                 "to": 20
               },{
                 "from": 20,
                 "to": 40
               },{
                 "from": 40,
                 "to": 50
               }
             ]
           },
           "aggs": {
             "all_tags": {
               "terms": {
                 "field": "price"
               },
               "aggs": {
                 "avg_price": {
                   "avg": {
                     "field": "price"
                   }
                 }
               }
             }
           }
         }
       }
    }

  • 相关阅读:
    51nod1089(最长回文子串之manacher算法)
    51nod1088(最长回文子串)
    51nod1256(乘法逆元)
    51nod1085(01背包)
    51nod1079(中国剩余定理)
    数据的特征工程
    30种提高mysql处理速度的方法
    机器学习资料
    python3.6安装-windows
    python import sklearn出错 "ImportError: DLL load failed: 找不到指定的模块。
  • 原文地址:https://www.cnblogs.com/wshcn/p/8150511.html
Copyright © 2011-2022 走看看