接续上篇,本篇介绍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. java多线程通过管道流实现不同线程之间的通信

    java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据.通过使用管道,实现不同线程间的通信,而不必借助类似 ...

  2. Flyweight_pattern--reference

    http://en.wikipedia.org/wiki/Flyweight_pattern In computer programming, flyweight is a software desi ...

  3. DNN学习笔记 最简单的皮肤制作

    说明: 在学习DNN时,使用的版本为 DNN8.参考资料:http://www.dnnsoftware.com/docs/designers/creating-themes/index.html 制作 ...

  4. dom父节点

  5. tomcat多站点部署

    我们可能会有这种场景,一个tomcat想部署两个web工程,说白了就是公用一个端口,那怎么办呢?就是多站点部署,具体步骤如下(这里以linux平台举例): 1)先修改server.xml(conf/s ...

  6. XHML教会我的一些东西-3

    在寒假期间,隔几天就同学聚会,每天都是起床困难户.每天都想着要完成任务,要学习新的东西.但是总是被自己惰性占为上风.感觉自己很没用,但是又继续堕落.真的不能理解自己.呵呵.... 在放假一段时间之后, ...

  7. agc027D - Modulo Matrix(构造 黑白染色)

    题意 题目链接 构造一个\(n * n\)的矩阵,要求任意相邻的两个数\(a,b\),使得\(max(a,b) \% min(a,b) \not = 0\) Sol 我的思路: 假设\(mod = 1 ...

  8. jQuery实现焦点图[兼容ie7+]

    HTML: <div class="freehand" id="freehand"> <h1>宠物手绘</h1> <d ...

  9. 运行python文件报SyntaxError:Non-ASCII character '\xe7'

    以下是报错内容: 在文件页头加上: #coding=uft-8 ~解决了~ 记录一下(捂脸)

  10. MyBatis中sql语句

    一.select <!-- 查询学生,根据id --> <select id="getStudent" parameterType="String&qu ...