接续上篇,本篇介绍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. stm32串口学习(一)

    串口在工作中经常用到,今天我们从零开始学习stm32的串口编程(利用库函数). 先从最简单的情况开始,假设我们要实现的功能就是串口发送一个字节,不考虑接收,也不考虑中断. 那么要解决两个问题: 1 串 ...

  2. GitKraken使用教程-基础部分(2)

    3. 修改用户名 为了方便项目中代码的管理,需要重新编辑用户名. 点击右上角的图像即可看到如下图 3‑1所示的下拉菜单,鼠标悬于Profile上,会出现一个Edit按钮. 图 3‑1 编辑个人信息 点 ...

  3. HDU 4357——String change——————【规律题】

    String change Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. Java取得一个对象里所有get方法和set方法, 读取某个类下所有变量的名称

    所有get方法和set方法public void getMethod(Object obj){ Class clazz=obj.getClass();//获得实体类名 Field[] fields = ...

  5. xml schema数据类型

    1.简单数据类型 (1)内置简单数据类型 schema中定义了一些简单数据类型,包括primitive原始数据类型和derived派生数据类型,这些类型都是schema中使用的 最基本的数据类型,我们 ...

  6. 翻String.Format源码发现的新东西:StringBuilderCache

    起因: 记不清楚今天是为毛点想F12看String.Format的实现源码了,反正就看到了下图的鸟东西: 瞬间石化有没有,StringBuilder还能这么获取? 研究StringBuilderCac ...

  7. poj 3107 删点最大分支最小

    http://poj.org/problem?id=3107 这实际上就是找重心,在之前有做过:http://www.cnblogs.com/qlky/p/5780933.html #include ...

  8. CSS超链接的常见设置

    一般对超连接常见的设置,就是设置文字大小,下划线,颜色等等. 先讲解一下,超链接的四种状态 /* 未被访问的链接 */ a:link {color:#FF0000;} /* 已被访问的链接 */ a: ...

  9. 使用js来执行全屏

    当用户按下F11事件,浏览器为触发自身全屏功能,这个过程我们一般是不可控制的,即使是监听了F11的键盘事件,退出全屏的时候,我们也捕捉不到退出全屏触发的事件.所以,我们就用程序自己去实现F11的功能, ...

  10. 用canvas绘制一个简易时钟

    在见识了html5中canvas的强大,笔者准备制作一个简易时钟. 下面就是成果啦,制作之前我们先分析一下,绘制一个时钟需要做哪些准备. 一 . 1.首先这个时钟分为表盘,指针(时针,分针,秒针)和数 ...