ElasticSearch---桶聚合

根据salary工资分组聚合(显示出相同工资人数)

GET my_hello/_search

{

  "size":0,

  "aggs":{

    "group_count":{

      "terms":{"field":"salary"}

    }

  }

}

根据salary工资分组聚合(显示出相同工资人数),并且统计出每组的平均年龄

GET my_hello/_search

{

  "size":0,

  "aggs":{

    "group_count":{

      "terms":{"field": "salary"},

      "aggs": {

        "avg_age": {

          "avg": {"field": "age"}

        }

      }

    }

  }

}

统计每个部门dep有多少人(这时的dep要用dep.keyword)

GET my_hello/_search

{

  "size":0,

  "aggs":{

    "group_count":{

      "terms": {"field": "dep"}

      

      }

    }

  }

结果显示:

{

  "error": {

    "root_cause": [

      {

        "type": "illegal_argument_exception",

        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [dep] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."

      }。。。。。。。。。。。

这里提示我们要用keyword,正确代码:

GET my_hello/_search

{

  "size":0,

  "aggs":{

    "group_count":{

      "terms": {"field": "dep.keyword"}

      

      }

    }

  }   

Filter Aggregation Filter聚合用于过滤器聚合,把满足过滤器条件的文档分到一组。
计算男人的平均年龄 也就是统计gender字段包含关键字“男”的文档的age平均值。

统计男人人数并计算平均年龄

GET my_hello/_search

{

  "size":0,

  "aggs":{

    "group_count":{

      "filter": {

        "term":{"gender":"男"}

        },

        "aggs":{

          "avg_age":{

            "avg": {"field": "age"

            }

          }

        }

      }

    }

  }

新建数据:

PUT /logs/message/_bulk?refresh

{ "index" : { "_id" : 1 } }

{ "body" : "warning: page could not be rendered" }

{ "index" : { "_id" : 2 } }

{ "body" : "authentication error" }

{ "index" : { "_id" : 3 } }

{ "body" : "warning: connection timed out" }

统计body中包含“error”和“warning”的个数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值