• 简单聚合查询


    首先将文本field的fielddata属性设置为true,否则会报错
    PUT /ecommerce/_mapping/product
    {
      "properties" : {
        "tags" : {
          "type" : "text",
          "fielddata" : true
        }
      }
    }

    1、计算每个tag下的商品数量
    GET /ecommerce/product/_search
    {
      "size" : 0,
      "aggs" : {
        "all_tags" : {
          "terms" : { "field" : "tags" }
        }
      }
    }

    2、对名称中包含yagao的商品,计算每个tag下的商品数量
    GET /ecommerce/product/_search
    {
      "size" : 0,
      "query" : {
        "match" : {"name" : "yagao"}
      },
      "aggs" : {
        "all_tags" : {
          "terms" : { "field" : "tags"}
        }
      }
    }

    3、先分组,再计算每组的平均值。计算每个tag下的商品的平均价格
    GET /ecommerce/product/_search
    {
      "size" : 0,
      "aggs" : {
        "group_by_tags" : {
          "terms" : { "field" : "tags" },
          "aggs" : {
            "avg_price" : {
              "avg" : { "field" : "price" }
            }
          }
        }
      }
    }

    4、计算每个tag下的商品的平均价格,并且按照平均价格降序排序
    GET /ecommerce/product/_search
    {
      "size" : 0,
      "aggs" : {
        "all_tags" : {
          "terms" : {"filed" : "tags","order" : {"avg_price" : "desc"}},
          "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" : 60
              }
            ]
          },
          "aggs" : {
            "group_by_tags" : {
              "terms" : {"field" : "tags"},
              "aggs" : {
                "avg_price" : {
                  "avg" : {
                    "field" : "price"
                  }
                }
              }
            }
          }
        }
      }
    }

  • 相关阅读:
    Element学习
    top level element is not completed
    IntelliJ IDEA 使用心得与常用快捷键
    MVC下的DAO接口类和SERVICE接口类区别?
    Emmet初探2
    Servlet和Android网络交互基础(3)
    A-Frame WebVR开发新手教程
    重温java中的String,StringBuffer,StringBuilder类
    Android 常见面试题
    <html>
  • 原文地址:https://www.cnblogs.com/qinjf/p/8445655.html
Copyright © 2020-2023  润新知