zoukankan      html  css  js  c++  java
  • Elasticsearch学习之嵌套聚合,下钻分析,聚合分析

    1. 计算每个tag下的商品数量

      GET /ecommerce/product/_search
      {
        "aggs": {
          "group_by_tags": {
            "terms": { "field": "tags" }
          }
        }
      }

    2. 将文本field的fielddata属性设置为true

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

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

    3. 对名称中包含yagao的商品,计算每个tag下的商品数量

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

    4. 先分组,再算每组的平均值,计算每个tag下的商品的平均价格

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

    5. 计算每个tag下的商品的平均价格,并且按照平均价格降序排序

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

    6. 按照指定的价格范围区间进行分组,然后在每组内再按照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": {
              "group_by_tags": {
                "terms": {
                  "field": "tags"
                },
              "aggs": {
                "average_price": {
                  "avg": {
                      "field": "price"
                   }
                 }
               }
              }
           }
          }
         }
       }

  • 相关阅读:
    资源链接
    python pip下载速度慢的解决方法
    淘宝 NPM 镜像
    python学习链接
    Linux升级python3之后yum不能正常使用解决方法一:重新配置yum源
    rand和srand的用法
    static与volatile的用法
    CentOS 7
    C++类(Class)总结
    简单的linux命令
  • 原文地址:https://www.cnblogs.com/sunfie/p/7019835.html
Copyright © 2011-2022 走看看