elasticsearch的聚合查询

elasticsearch教程

聚合基本格式
GET comment/_search
{
  "size": 0,
  "aggs": {
    "NAME": {
      "AGG_TYPE": {}
    }
  }
}

其中NAME表示当前聚合的名字,可以取任意合法的字符串,AGG_TYPE表示聚合的类型,常见的为分为多值聚合和单值聚合

例子

GET comment/_search
{
 "size": 0, 
  "aggs": {
    "sum_all": {
      "sum": {
        "field": "likeCount"
      }
    }
  }
}

上面的例子表示查询当前库里面的likeCount的和,返回结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4623,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "sum_all" : {
      "value" : 598.0
    }
  }
}

返回结果中默认会包含命中的document,所以需要把size指定为0,结果中的sum_all为请求中指定的名字。

Metrics

metrics主要是一些单值的返回,像avg、max、min、sum、stats等这些计算。

max

比如计算index里面最多的点赞数是多少

GET comment/_search
{
 "size": 0, 
  "aggs": {
    "max_replay": {
      "max": {
        "field": "likeCount"
      }
    }
  }
}
stats

常用的一些统计信息,可以用stats,比如查看某个字段的,总数,最小值,最大值,平均值等,比如查看document中新闻回复量的基本情况

GET comment/_search
{
 "size": 0, 
  "aggs": {
    "max_replay": {
      "stats": {
        "field": "likeCount"
      }
    }
  }
}

返回结果为:

{
  "took" : 48,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4623,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "max_replay" : {
      "count" : 4623,
      "min" : 0.0,
      "max" : 4.0,
      "avg" : 0.12935323383084577,
      "sum" : 598.0
    }
  }
}

Bucket

桶类似于sql里面的group by,使用Bucket会对内容进行分桶

terms

​ 利用terms分桶之后,可以查看数据的分布,比如可以查看被评论资源有多少,每个资源有多少记录,size是用来指定返回最多的几个分类

GET comment/_search
{
  "size": 0,
  "aggs": {
    "myterms": {
      "terms": {
        "field": "ownerType",
        "size": 100
      }
    }
  }
}

返回

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4623,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "myterms" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 2106
        },
        {
          "key" : 2,
          "doc_count" : 1390
        },
        {
          "key" : 5,
          "doc_count" : 667
        },
        {
          "key" : 4,
          "doc_count" : 154
        },
        {
          "key" : 3,
          "doc_count" : 131
        },
        {
          "key" : 6,
          "doc_count" : 102
        },
        {
          "key" : 7,
          "doc_count" : 73
        }
      ]
    }
  }
}

查询和聚和的组合

有了查询和聚合,我们就可以对查询的结果做聚合,比如我想查看评论中包含【我们】的记录各个来源有多少,就可以像下面这样查询

GET comment/_search
{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "我们"
          }
        }
      ]
    }
  },
  "aggs": {
    "cate": {
      "terms": {
        "field": "ownerType"
      }
    }
  }
}

返回

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 260,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "cate" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 1,
          "doc_count" : 160
        },
        {
          "key" : 2,
          "doc_count" : 64
        },
        {
          "key" : 5,
          "doc_count" : 26
        },
        {
          "key" : 3,
          "doc_count" : 4
        },
        {
          "key" : 4,
          "doc_count" : 4
        },
        {
          "key" : 6,
          "doc_count" : 1
        },
        {
          "key" : 7,
          "doc_count" : 1
        }
      ]
    }
  }
}


博主个人博客网站:奇想派

本文原创作者:奇想派、一名努力分享的程序员。

文章首发平台:微信公众号【编程达人】

qrcode_for_gh_ef30b7fd9e6f_344

原创不易!各位小伙伴觉得文章不错的话,不妨关注公众号,进行点赞(在看)、转发三连走起!谢谢大家!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奇怪的守护神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值