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"
                   }
                 }
               }
             }
           }
         }
       }
    }

  • 相关阅读:
    Zjnu Stadium(hdu3047带权并查集)
    cocos2d-x结合cocosbuilder,不同屏幕适配小结
    分布式爬虫系统设计、实现与实战:爬取京东、苏宁易购全网手机商品数据+MySQL、HBase存储
    Generating RSA keys in PKCS#1 format in Java--转
    nodejs安装node-rsa遇到的问题及解决
    spring-redis-data的一个坑
    node-rsa加密,java解密调试
    MySQL 四种事务隔离级别详解及对比--转
    从实际案例聊聊Java应用的GC优化--转
    动态可缓存的内容管理系统(CMS)
  • 原文地址:https://www.cnblogs.com/wshcn/p/8150511.html
Copyright © 2011-2022 走看看