接续上篇,本篇介绍elasticsearch聚合查询,使用python库elasticsearch-dsl进行聚合查询操作。

7.3、聚合查询

高阶概念

  • Buckets(桶/集合):满足特定条件的文档的集合
  • Metrics(指标):对桶内的文档进行统计计算(例如最小值,求和,最大值等)

    • 新建一张测试表

       PUT cars
      {
      "mappings": {
      "transactions":{
      "properties": {
      "price":{
      "type": "integer"
      },
      "color":{
      "type": "text",
      "fielddata": true
      },
      "make":{
      "type": "text",
      "fielddata": true
      },
      "sold":{
      "type": "date",
      "format": "yyyy-MM-dd"
      }
      }
      }
      }
      }

      插入数据

       POST /cars/transactions/_bulk
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
      { "index": {"_index": "cars", "_type": "transactions"}}
      { "price" : , "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
    • 查询哪个颜色的汽车销量最好(按颜色分类)
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "popular_colors": {
      "terms": {
      "field": "color"
      }
      }
      }
      }
       s = Search(index='cars')
      a = A("terms", field="color")
      s.aggs.bucket("popular_color", a)
      response = s.execute()

      或者

       s.aggs.bucket("popular_color", "terms", field="color")
    • 查询每种颜色车的平均价格
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "colors": {
      "terms": {
      "field": "color"
      },
      "aggs": {
      "avg_price": {
      "avg": {
      "field": "price"
      }
      }
      }
      }
      }
      }
       s = Search(index='cars')
      a1 = A("terms", field="color")
      a2 = A("avg", field="price")
      s.aggs.bucket("colors", a1).metric("avg_price", a2)
      response = s.execute()

      或者

       s = Search(index='cars')
      s.aggs.bucket("colors", "terms", field="color").metric("avg_price", "avg", field="price")
      response = s.execute()
    • 先按颜色分,再按品牌分,再求每种品牌的均价
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "colors": {
      "terms": {
      "field": "color"
      },
      "aggs": {
      "make": {
      "terms": {
      "field": "make"
      },
      "aggs": {
      "avg_price": {
      "avg": {
      "field": "price"
      }
      }
      }
      }
      }
      }
      }
      }
       s = Search(index='cars')
      s.aggs.bucket("colors", "terms", field="color")
      s.aggs["colors"].bucket("make", "terms", field="make")
      s.aggs["colors"].aggs["make"].metric("avg_price", "avg", field="price")
      response = s.execute()
    • 先按颜色分,再按品牌分,再求每种品牌的最高和最低价
       GET cars/transactions/_search
      {
      "size": ,
      "aggs": {
      "colors": {
      "terms": {
      "field": "color"
      },
      "aggs": {
      "make": {
      "terms": {
      "field": "make"
      },
      "aggs": {
      "min_price": {
      "min": {
      "field": "price"
      }
      },
      "max_price": {
      "max": {
      "field": "price"
      }
      }
      }
      }
      }
      }
      }
      }
       s = Search(index='cars')
      s.aggs.bucket("colors", "terms", field="color")
      s.aggs["colors"].bucket("make", "terms", field="make")
      s.aggs["colors"].aggs["make"].metric("min_price", "min", field="price")
      s.aggs["colors"].aggs["make"].metric("max_price", "max", field="price")
      response = s.execute()
    • 未完待续...

elasticsearch-dsl聚合-1的更多相关文章

  1. ElasticSearch实战系列五: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合

    Title:ElasticSearch实战系列四: ElasticSearch的聚合查询基础使用教程之度量(Metric)聚合 前言 在上上一篇中介绍了ElasticSearch实战系列三: Elas ...

  2. Elasticsearch(8) --- 聚合查询(Metric聚合)

    Elasticsearch(8) --- 聚合查询(Metric聚合) 在Mysql中,我们可以获取一组数据的 最大值(Max).最小值(Min).同样我们能够对这组数据进行 分组(Group).那么 ...

  3. Elasticsearch(9) --- 聚合查询(Bucket聚合)

    Elasticsearch(9) --- 聚合查询(Bucket聚合) 上一篇讲了Elasticsearch聚合查询中的Metric聚合:Elasticsearch(8) --- 聚合查询(Metri ...

  4. Elasticsearch 之聚合分析入门

    本文主要介绍 Elasticsearch 的聚合功能,介绍什么是 Bucket 和 Metric 聚合,以及如何实现嵌套的聚合. 首先来看下聚合(Aggregation): 什么是 Aggregati ...

  5. Elasticsearch系列---聚合查询原理

    概要 本篇主要介绍聚合查询的内部原理,正排索引是如何建立的和优化的,fielddata的使用,最后简单介绍了聚合分析时如何选用深度优先和广度优先. 正排索引 聚合查询的内部原理是什么,Elastich ...

  6. Elasticsearch DSL中Query与Filter的不同

    Elasticsearch支持很多查询方式,其中一种就是DSL,它是把请求写在JSON里面,然后进行相关的查询. 举个DSL例子 GET _search { "query": { ...

  7. ElasticSearch 的 聚合(Aggregations)

    Elasticsearch有一个功能叫做 聚合(aggregations) ,它允许你在数据上生成复杂的分析统计.它很像SQL中的 GROUP BY 但是功能更强大. Aggregations种类分为 ...

  8. ElasticSearch - 信息聚合系列之聚合过滤

    摘要 聚合范围限定还有一个自然的扩展就是过滤.因为聚合是在查询结果范围内操作的,任何可以适用于查询的过滤器也可以应用在聚合上. 版本 elasticsearch版本: elasticsearch-2. ...

  9. [elk]elasticsearch dsl语句

    例子1 统计1,有唱歌兴趣的 2,按年龄分组 3,求每组平均年龄 4,按平均年龄降序排序 sql转为dsl例子 # 每种型号车的颜色数 > 1的 SELECT model,COUNT(DISTI ...

  10. elasticsearch DSL查询

    总结一个DSL的用法吧,语法网上查去,只记录一点心得,也是研究了半天,太麻烦了 先附上python代码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ fro ...

随机推荐

  1. Offic转换pdf 之asposeDLL插件

    //excel转换 Workbook workbook = new Workbook(HttpContext.Current.Server.MapPath(docpath + "/" ...

  2. [Java][Liferay] 解决在Linux系统中liferay-ext项目无法卸载的问题

    今天遇到liferay-ext无法卸载,log中显示卸载了,但是在App Manager中依然可以看到安装过的ext,其中一个原因是webapps下面的**-ext文件夹的权限只有root才能修改,将 ...

  3. JQ单双引号转义

    var temp = "${row.address_province}"; alert(temp);——————即变量temp alert("\'"+temp+ ...

  4. 增加ssh无密码信任连接的安全性

    为了方便系统管理或者服务器运维自动化,我们通常要在服务器间做ssh无密码信任连接. 环境:目标主机    centos7    192.168.150.110操作主机    centos7-cn 19 ...

  5. Android NDK 入门与实践

    NDK 是什么 NDK 全称 Native Development Kit,可以让您在 Android 应用中调用 C 或 C++ 代码的工具. NDK 好处 1.NDK 可以生成 .so 文件, 方 ...

  6. Zamplus 晶赞天机

    类型: 定制服务 软件包: car/vehicle integrated industry solution collateral tourism 联系服务商 产品详情 解决方案 概要 DMP:通常称 ...

  7. php的yii框架开发总结9

    这一篇讲解怎么实现的自动发邮件的功能,我在网上查了很多资料,很多都是用定时检测来实现的,我试过,效率太低,网站也卡了. 后来就写了一个.bat文件来实现刷新页面,用了windows的定时任务定时来运行 ...

  8. Spark master节点HA配置

    Spark master节点HA配置 1.介绍 Spark HA配置需要借助于Zookeeper实现,因此需要先搭建ZooKeeper集群. 2.配置 2.1 修改所有节点的spark-evn.sh文 ...

  9. MVC学习笔记:MVC实现用户登录验证ActionFilterAttribute用法并实现统一授权

    在项目下新建一个文件夹来专门放过滤器类,首先创建一个类LoginFilter,这个类继承ActionFilterAttribute.用来检查用户是否登录和用户权限.: using System; us ...

  10. 磁盘空间满了之后MySQL会怎样

    大多数用户在对于磁盘进行分区的时候都是习惯性的不给系统盘预留很大空间,其实这并不是一个好习惯.因为系统分区并不像我们想象的那样会仅仅安装一个操作系统,系统分区多数还是会承载操作系统主要应用软件安装任务 ...