接续上篇,本篇介绍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. 第一次尝试用Open Live Writer写日志

    注册博客园很久了,一直没怎么用,今天登陆了一下,看到这个写日志工具,看着不错,试一下

  2. mysql数据库免安装版的配置过程

    1,从mysql官方网站下载免安装版本与自己电脑位数相同的mysql版本. 链接:https://www.mysql.com/ 2,将包解压到自定义的目录下 (例:D:\mysql-5.7.23-wi ...

  3. FastDFS 基础知识

    FastDFS是一个开源的轻量级分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过专有API对文件进行存取访问,不支持POSIX接口方式,不能mount ...

  4. C#开发短信的方法和简介(转)

    http://ce.sysu.edu.cn/hope2008/Education/ShowArticle.asp?ArticleID=6337(来自) 自己收藏哈子

  5. #CSS的盒子模型、元素类型

    CSS的盒子模型.元素类型   本文首先介绍了CSS元素的统一内部结构模型,盒子模型:然后介绍了CSS元素基于不同分类标准定义的元素类型,包括基于不同内容设置方式定义的replaced元素和non-r ...

  6. AngularJS 指令解析(二)

    AngularJS 指令解析(二) 第一篇我们讲过了作用域(scope)这块内容,现在我们进入正题,讲AngularJS的指令. 什么是指令? 这里我们引用官方的一句话: Custom directi ...

  7. Design Pattern ->Prototype

    Layering & Contract Philosophy With additional indirection Prototype The example code is as foll ...

  8. Android TextView之空格占位法

    在Android布局中进行使用到空格,为了实现文字的对齐.具体要怎么使用了? •请忽视文中‘& #160’中&和#之间的空格 空格: & #160; 窄空格: & #8 ...

  9. 属性动画 常用属性及View常用方法

    View类中,常用于属性动画的属性: translationX and translationY: These properties control where the View is located ...

  10. python 生成随机图片验证码

    1.安装pillow模块 pip install pillow (1)创建图片 from PIL import Image #定义使用Image类实例化一个长为400px,宽为400px,基于RGB的 ...